开发者

Why does my code stop after "con.Open()"?

开发者 https://www.devze.com 2022-12-15 02:19 出处:网络
In my code, why does nothing execute after con.Open(). If I step through each step, it ends the Form1_Load event after con.Open().

In my code, why does nothing execute after con.Open().

If I step through each step, it ends the Form1_Load event after con.Open().

Imports System.Data.OleDb

Public Class Form1

Dim con As OleDbConnection
Dim strCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\testDB.accdb"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    con = New OleD开发者_如何转开发bConnection(strCon)

    con.Open()
    MsgBox("con open")

    con.Close()
    MsgBox("con closed")

End Sub
End Class


Sounds like an Error is begin raised.

Have you tried framing your code with OnError Goto Handler type code? See the On Error Statement.

If this is VB.NET, trying catching an exception with Try ... Catch. Then set a breakpoint on the Catch statements to find out what the error is.


After installing the MS Access Database Engine (to use Microsoft.ACE.OLEDB.12.0), my program still did not work. I still had the error:

"The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local muoteachine."

I also had to change the advanced compile configuration because I am running on Windows 7 64 bit and may have "...locked the processor type to 32-bit".

How to change the settings:

1.Tools -> Options.
2.Check "Show all settings".
3.In the tree-view on the left hand side, select "Projects and Solutions".
4.In the options on the right, check the box that says, "Show advanced build configuraions."
5.Click OK.
6.Go to Build -> Configuration Manager...
7.In the Platform column select "new" in the drop down list.
8. Select "x86" under first dropdown box.
9. "Ok"

More info here: Microsoft.ACE.OLEDB.12.0 provider is not registered


Even if you have proper try catch block some time it take several seconds for the con.Open() to throw an exception since it's waiting for the server. Following error takes a while to throw an exception and it will appear as if your Step Into disappeared. Be patient!

"ORA-12170: TNS:Connect timeout occurred"


Imports System.Data.Odbc

Public Class Form11

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim a1, a2, a6, a3, a4, a5, a7 As String
    ' Dim a2, a6 As Integer
    a1 = txtname.Text
    a3 = txtaddress.Text
    a4 = txtcity.Text
    a5 = txtmail.Text
    a7 = txtbalance.Text
    a2 = txtmob.Text
    a6 = txtvehicles.Text
    Dim con As New OdbcConnection("DSN = crusher_schema")
    con.Open()
    MsgBox("Databse Connected", MsgBoxStyle.Information, "add")
    Dim cmd As New Odbc.OdbcCommand("insert into customer values('" & a1 & "','" & a2 & "','" & a3 & "','" & a4 & "','" & a5 & "','" & a6 & "','" & a7 & "', con")
    Dim r As Integer
    r = cmd.ExecuteNonQuery
    If r = 1 Then
        MessageBox.Show("Succesfully inserted" & r)
    Else
        MessageBox.Show("Not inserted" & r)
    End If!

Form.vb

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号