首页 > 解决方案 > Vb.net NO 值给一个或多个给定参数

问题描述

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 中工作

没有为一个或多个给定参数指定值

填充数据表时出现错误......为什么......我该如何解决这个问题。请帮忙

标签: vb.net

解决方案


您的问题是您正在传递 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

推荐阅读