必须声明标量变量“@ employeeid” [英] Must declare the scalar variable "@employeeid"

查看:259
本文介绍了必须声明标量变量“@ employeeid”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Public Sub SqlSelectTimesAwaitingApproval(dgv as datagridview, listOfEmployees As List(Of integer))

    Dim con As New SqlConnection(My.Settings.AlphaCureCRMConnectionString)
    Dim cmd As New SqlCommand()
    Dim ds As New DataSet
    Try
        cmd.Connection = Con
        For Each intId As Integer In listOfEmployees
            Con.Open()
            cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = intId
            cmd.CommandText = "SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID"
            Dim da As New SqlDataAdapter() With
            {.SelectCommand = New SqlCommand(cmd.CommandText, con)}
            da.Fill(ds, "Table")
            con.Close()
            da.Dispose()
        Next
        Dgv.DataSource = ds.Tables("Table")
        ds.Dispose()
    Catch ex As Exception
        MsgBox(Ex.Message)
    Finally
        Cmd.Dispose()
    End Try
End Sub





无法弄清楚为什么我收到此错误,参数在被调用之前被定义。我已经工作了一段时间,但必须改变一些东西,现在我根本无法使用它。



我尝试过:



我试过大多只是改变代码,但没有用。



Cannot figure out why i am getting this error, the parameter is being defined before it's called. I have had this working for a while but something must of changed and now i can't get this to work at all.

What I have tried:

I have tried mostly just changing around the code as it is but to no avail.

推荐答案

将命令参数添加到原始命令,而不是通过适配器填充DataSet的命令:

You add the command parameter to the original command, not the one you fill your DataSet from via the adapter:
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = intId
                cmd.CommandText = "SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID"
                Dim da As New SqlDataAdapter(cmd) With 
                {.SelectCommand = New SqlCommand(cmd.CommandText, con)}
                da.Fill(ds, "Table")



试试这个:


Try this:

cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = intId
cmd.CommandText = "SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID"
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds, "Table")


cmd.Parameters.Add(@ EmployeeID,SqlDbType.Int).Value = intId

cmd。 CommandText =SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID

Dim da As New SqlDataAdapter()with

{ .SelectCommand =新的SqlCommand(cmd.CommandText,con )}



也许你的意思是......



{ .SelectCommand = cmd }
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = intId
cmd.CommandText = "SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID"
Dim da As New SqlDataAdapter() With
{.SelectCommand = New SqlCommand(cmd.CommandText, con)}

Maybe you meant...

{.SelectCommand = cmd}


这篇关于必须声明标量变量“@ employeeid”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆