【问题标题】:OleDbException Unhandled (No value given for one or more required parameters.) -Visual Studio 2010OleDbException 未处理(没有为一个或多个必需参数提供值。)-Visual Studio 2010
【发布时间】:2015-04-03 04:01:55
【问题描述】:

我正在做一个铁路预订系统项目,我希望用户可以访问他的预订历史。下面的代码访问数据库中两个表的记录。当我只为一个表运行此代码(从一个表访问记录)时,它工作正常,但是当我为两个表运行此代码时,它给我一个错误,说 OleDbexception 未处理(没有为更多必需参数提供值)在

da.Fill(ds, "Table2")

我知道这条线是错误的,因为它只适用于一张桌子,但我不知道如何为两张桌子做这件事。请帮忙 !谢谢 !

导入 System.Data.OleDb

公开课表12

Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\AMEN\Documents\Railway.accdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MyConn = New OleDbConnection
    MyConn.ConnectionString = connString
    ds = New DataSet
    tables = ds.Tables
    da = New OleDbDataAdapter("Select P.Tnumber, P.Name , P.Age ,T.PNR_Number,  P.Train_Name , P.SeatNo , P.Berth , P.Coach_Number , T.Starting_Point , T.Destination , T.Departure , T.Arrival , T.Fare From Table1,Table2 Where P.Train_Name = T.Train_Name and P.Phone= TextBox1.Text ", MyConn)
    da.Fill(ds, "Table2")

    Dim view As New DataView(tables(0))
    source1.DataSource = view
    DataGridView1.DataSource = view

End Sub

结束类

【问题讨论】:

    标签: database vb.net visual-studio-2010 oledb


    【解决方案1】:

    您的查询中有两个表要连接在一起。在您的查询中,我将在 Table1 为 T 且 Table2 为 P 的前提下继续。如果相反,只需更改它即可。

    话虽如此,试试这个:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using cn = New OleDbConnection(connString)
            cn.Open()
            Using da = New OleDbDataAdapter("SELECT P.Tnumber, P.Name , P.Age ,T.PNR_Number,  P.Train_Name , P.SeatNo , P.Berth , P.Coach_Number , T.Starting_Point , T.Destination , T.Departure , T.Arrival , T.Fare FROM Table1 T INNER JOIN Table2 P ON P.Train_Name = T.Train_Name WHERE P.Phone='" & TextBox1.Text & "'", cn)
                Dim dt = New DataTable
                da.Fill(dt)
                DataGridView1.DataSource = dt
            End Using
        End Using
    End Sub
    

    将 Using 块用于连接和数据适配器等一次性资源也是一个好主意。

    【讨论】:

    • 感谢您的回复。它说 OLeDbException 在 da.Fill(dt) 未处理。还说没有为一个或多个必需参数提供值。
    • 听起来您的查询语法可能有问题。如果您可以将查询复制并粘贴到查询工具中,用测试值替换 WHERE 子句中的实际值,然后查看它的内容。您的表是否命名为 Table1 和 Table2?
    • 是的,我的表被命名为 Table1 和 Table2
    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多