0

我尝试将值传递给数据适配器,但出现错误“并非所有变量都绑定”:

Dim strSql As String = "SELECT * FROM table WHERE A_DT>:a AND B_DT<:b"
Dim conn As New OleDb.OleDbConnection(strConn)
Dim datData As New OleDb.OleDbDataAdapter(strSql, CType(conn, OleDb.OleDbConnection))
Dim dtbdata As New DataTable
datData.SelectCommand.Parameters.Add(":a", OleDb.OleDbType.DBDate).Value = Date.Parse(A_Date)
datData.SelectCommand.Parameters.Add(":b", OleDb.OleDbType.DBDate).Value = Date.Parse(B_Date)
datData.Fill(dtbdata) '<--- Error happen in this line
4

1 回答 1

0

请注意,OleDb 不支持命名参数,您需要使用“?” 而不是命名参数,然后按顺序传递参数,您的代码应如下所示:

Dim strSql As String = "SELECT * FROM table WHERE A_DT>? AND B_DT<?"
Dim conn As New OleDb.OleDbConnection(strConn)
Dim datData As New OleDb.OleDbDataAdapter(strSql, CType(conn, OleDb.OleDbConnection))
Dim dtbdata As New DataTable
datData.SelectCommand.Parameters.Add(New OleDb.OleDbParameter("@a", Date.Parse(A_Date)))
datData.SelectCommand.Parameters.Add(New OleDb.OleDbParameter("@b", Date.Parse(B_Date)))
datData.Fill(dtbdata)

如果您仍然坚持使用命名参数,我建议您改用SqlClient

于 2013-10-02T08:11:01.220 回答