尝试使用 VB 从 ACCESS 数据库输出 Visual Web Developer 2008 中的数据

发布于 2024-12-14 01:12:04 字数 1325 浏览 0 评论 0原文

好的,我可以成功连接到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2024-12-21 01:12:04

您没有向 SQL 传递正确的值:

"SELECT [Title], [Description] FROM [Incidents] WHERE ([CustomerID] = textbox1.text)"

它将尝试查找等于数据库值“textbox1.text”的 CustomerID。由于这可能是错误的数据类型并且表/列不存在,因此您会收到错误。

您应该使用 参数化查询以避免SQL注入

Dim sql As String = "SELECT [Title], [Description] FROM [Incidents] WHERE ([CustomerID] = ?)"

Dim myCommand As New System.Data.OleDb.OleDbCommand(sql, conn)
myCommand.Parameters.Add("@p1", OleDbType.Integer, 4).Value = textbox1.text

如果 textbox1.text 中的值的类型不正确,这将失败 - 您应该验证并转换为正确的类型(我的代码假定为整数)。

You are not passing in a proper value to the SQL:

"SELECT [Title], [Description] FROM [Incidents] WHERE ([CustomerID] = textbox1.text)"

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.

Dim sql As String = "SELECT [Title], [Description] FROM [Incidents] WHERE ([CustomerID] = ?)"

Dim myCommand As New System.Data.OleDb.OleDbCommand(sql, conn)
myCommand.Parameters.Add("@p1", OleDbType.Integer, 4).Value = textbox1.text

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文