作为 .Net 编程的新用户,我试图了解此连接失败的原因。运行 catch 异常来通知我。有人可以指出我的代码有什么问题吗?非常感谢
Imports System.Data.OleDb
Public Class DbTest
Public buttonName As String
'Dim con1 As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\domain\storage2.accdb")
Private Sub racksfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
Dim connetionString As String
Dim oledbCnn As OleDbConnection
Dim oledbCmd As OleDbCommand
Dim sql As String
connetionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\domain\storage2.accdb"
sql = "SELECT [Rack_code] FROM Racks Where [Rack_code] = '" & buttonName & "'"
oledbCnn = New OleDbConnection(connetionString)
Try
oledbCnn.Open()
oledbCmd = New OleDbCommand(sql, oledbCnn)
Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
While oledbReader.Read
MsgBox(oledbReader.Item(0) & " - " & oledbReader.Item(1) & " - " & oledbReader.Item(2))
End While
oledbReader.Close()
oledbCmd.Dispose()
oledbCnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class