尝试使用 VB 从 ACCESS 数据库输出 Visual Web Developer 2008 中的数据
好的,我可以成功连接到我的 Access 数据库,但我仍在学习如何输出该数据。这是失败的。
我正在设计一个页面,用户在其中输入客户 ID,然后获取该客户的事件列表。然后我希望用户能够添加有关该事件的调查。
我遇到的问题是显示这些数据的能力。我尝试过各种方法。最终,我希望能够将数据显示到列表框中,让他们选择它并进行调查,但我还没有那么远。这是我在连接上的内容以及我的尝试,但它无法输出数据:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sql As String = "SELECT [Title], [Description] FROM [Incidents] WHERE ([CustomerID] = textbox1.text)"
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\Users\Allen PC7\Documents\Visual Studio 2008\Projects\1-A SportsPro\App_Data\TechSupport.mdb"
Dim myCommand As New System.Data.OleDb.OleDbCommand(sql, conn)
conn.Open()
Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand.ExecuteReader()
Try
While myReader.Read()
Console.WriteLine(myReader.GetInt32(0).ToString() + ", " + myReader.GetString(1))
End While
Finally
myReader.Close()
conn.Close()
End Try
End Sub
我只是无法在控制台中显示任何内容,而是收到一条错误消息 Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand .ExecuteReader()...System.Data.OleDb.OleDbException:没有为一个或多个必需参数指定值。
谢谢
Ok I can successfully connect to my Access database but I am still learning on how to output that data. Here is the run down.
I am designing a page where the user inputs the customer ID and then gets a list of incidents for that customer. Then I would like the user to be able to add a survey about the incident.
Problem I am running into is the ability to display this data. I have tried various method. Eventually I want to be able to display the data into a listbox and have them select it and take the survey but I am not near that far yet. Here is what I have on the connection and my attempt which doesn't work to output the data:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sql As String = "SELECT [Title], [Description] FROM [Incidents] WHERE ([CustomerID] = textbox1.text)"
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\Users\Allen PC7\Documents\Visual Studio 2008\Projects\1-A SportsPro\App_Data\TechSupport.mdb"
Dim myCommand As New System.Data.OleDb.OleDbCommand(sql, conn)
conn.Open()
Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand.ExecuteReader()
Try
While myReader.Read()
Console.WriteLine(myReader.GetInt32(0).ToString() + ", " + myReader.GetString(1))
End While
Finally
myReader.Close()
conn.Close()
End Try
End Sub
I just can't get anything displayed in the console and instead get an error saying Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand.ExecuteReader()...System.Data.OleDb.OleDbException: No value given for one or more required parameters.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有向 SQL 传递正确的值:
它将尝试查找等于数据库值“textbox1.text”的
CustomerID
。由于这可能是错误的数据类型并且表/列不存在,因此您会收到错误。您应该使用 参数化查询以避免SQL注入。
如果
textbox1.text
中的值的类型不正确,这将失败 - 您应该验证并转换为正确的类型(我的代码假定为整数)。You are not passing in a proper value to the SQL:
It will try to look for
CustomerID
that is equal to the database value "textbox1.text". Since this is probably the wrong data type and a table/column that doesn't exist, you are getting the error.You should be using a parameterized query to avoid SQL Injection.
This will fail if the value in
textbox1.text
is not of the right type - you should both validate and convert to the right type (my code assumes an integer).