我尝试将值传递给数据适配器,但是我收到错误“并非所有变量都绑定”:
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
答案 0 :(得分: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。