Dim cmd As OleDbCommand = New OleDbCommand(Sql, con)
Dim strSql As String = "Select EmpName,Count(EmpName) from tblPO where OrderType='" &
"B2B" & "' and POExpireDate < @LogDate Group By EmpName"
Dim tstDate As DateTime = DateTime.Now
Dim dateAsString As String = tstDate.ToString("dd/MM/yy")
cmd.Parameters.AddWithValue("@LogDate", CType(dateAsString, String))
Dim dtb As New DataTable
Using dad As New OleDbDataAdapter(strSql, con)
dad.Fill(dtb)
End Using
con.Close()
我正在VB.NET中工作
给定一个或多个给定参数的NO值
在填充数据表时出现错误。为什么...我该如何解决。 请帮助
答案 0 :(得分:0)
您的问题是,您正在将strSql和连接传递给数据适配器,而不是传递包含参数的命令。改为传递命令
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Using blocks ensure that your database objects are
'Closed And Disposed even if there Is an error.
Dim dtb As New DataTable
Using con As New OleDbConnection("Your connection string")
Dim strSql As String = "Select EmpName,Count(EmpName) from tblPO where OrderType = 'B2B' and POExpireDate < @LogDate Group By EmpName;"
Using cmd As OleDbCommand = New OleDbCommand(strSql, con)
cmd.Parameters.Add("@LogDate", OleDbType.Date).Value = DateTime.Now
'On the next line pass the command, no need to pass connection
'because it has already been passed to the constructor of the command
Using dad As New OleDbDataAdapter(cmd)
dad.Fill(dtb)
End Using
End Using
End Using
End Sub