执行时出错 [英] error while executing
问题描述
你好,
我使用ms访问作为我的数据库将它链接到visual studio。我在访问中创建了一个表,并希望通过单击搜索按钮在Visual Studio的其中一个文本框中显示该信息。
这是我的代码,我是使用:
Imports System.Data.OleDb
Public Class Form1
Dim provider As String
Dim dataFile As String
Dim connString As String
Public dr As OleDbDataReader
Public myConnection As OleDbConnection = New OleDbConnection
私有子Form1_Load(发件人为对象,e为EventArgs)处理MyBase.Load
provider =Provider = Microsoft.ACE.OLEDB.12.0;数据源= Hardness.accdb
dataFile =C:\ Users \ new try \Hardness.accdb
connString = provider&dataFile
myConnection。 ConnectionString = connString
myConnection.Open()
myConnection.Close()
End Sub
Private Sub Button3_Click(发送者为对象,e为EventArgs)处理Button3.Click
myConnection.Open()
TextBox2.Clear()
Dim str As String
str =SELECT * FROM Items WHERE(Material =' &TextBox1.Text&')
Dim cmd As OleDbCommand = New OleDbCommand(str,myConnection)
dr = cmd.ExecuteReader
当dr.Read()
TextBox2.Text = dr(Hardness)。ToString
myConnection.Close()
结束时
End Sub
结束课程
现在我点击运行按钮它给出的错误是:
System.Data.dll中发生了'System.Data.OleDb.OleDbException'类型的未处理异常
附加信息:不是有效的文件名。
有人,请看那里的错误...
hello there,
I am using ms access as my database to link it to visual studio. I have made a table in access and want to display that info in visual studio in one of the text boxes by clicking the search button.
this is my code which i have used:
Imports System.Data.OleDb
Public Class Form1
Dim provider As String
Dim dataFile As String
Dim connString As String
Public dr As OleDbDataReader
Public myConnection As OleDbConnection = New OleDbConnection
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =Hardness.accdb"
dataFile = "C:\Users\new try\Hardness.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
myConnection.Close()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
myConnection.Open()
TextBox2.Clear()
Dim str As String
str = "SELECT * FROM Items WHERE (Material = '" & TextBox1.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
TextBox2.Text = dr("Hardness").ToString
myConnection.Close()
End While
End Sub
End Class
Now when i am hitting the run button it is giving error which is:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Not a valid file name.
Can someone, please see whats wrong in there...
推荐答案
您好,
您的连接字符串看起来不正确。
查看一些示例MSAccss / OLEDB连接字符串,并解释其属性。
Microsoft ACE OLEDB 12.0 [ ^ ]
在你的例子中,如果你改变下面的2行应该让你更接近解决:
Hi,
Your connection string does not look right.
Have a look at some example MSAccss/OLEDB connection strings, with an explanation of their properties.
Microsoft ACE OLEDB 12.0[^]
In your example, if you change the 2 lines below should get you closer to resolving:
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\new try\Hardness.accdb"
connString = provider
...希望它有所帮助。
... hope it helps.
这篇关于执行时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!