必须声明标量变量“ @SSTGroupID”

时间:2019-11-25 07:06:08

标签: vb.net visual-studio scalar

Private Function GetSvcType(ByVal oCommand As OleDbCommand, ByVal SSTGroupID As Integer) As DataTable
    Dim sSQL As New StringBuilder

    sSQL.AppendLine(" Select SSTServiceTypeID AS ID, SSTServiceTypeName AS Name ")
    sSQL.AppendLine(" from fgen_SSTServiceType (nolock) ")
    sSQL.AppendLine(" Where 1=1 AND Disabled = 0 ")
    sSQL.AppendLine(" AND fgen_SSTServiceType.SSTGroupID = @SSTGroupID ")
    oCommand.Parameters.AddWithValue("@SSTGroupID", SSTGroupID)
    Return GetDataTable(sSQL.ToString)
End Function
Private Function GetDataTable(ByVal SQL As String) As DataTable
    Dim oConn As OleDbConnection = New OleDbConnection(_strConnection)
    Dim oCommand As New OleDbCommand("", oConn)
    oCommand.Connection.Open()
    oCommand.CommandText = SQL
    oCommand.Parameters.Clear()
    Dim oDataTable As New DataTable
    Dim oDataAdapter As New OleDbDataAdapter(oCommand)
    oDataAdapter.Fill(oDataTable)
    If oDataTable.Rows.Count > 0 Then
      GetDataTable = oDataTable
    Else
      GetDataTable = Nothing
    End If
    oCommand.Connection.Close()
    oCommand.Dispose()

  End Function

我一直在搜索数小时,似乎找不到解决方案。我需要您的帮助,谢谢

我已经更新了我的问题,其中包括GetDataTable函数。请看看谢谢。

2 个答案:

答案 0 :(得分:0)

您的命令从不从StringBuilder获取文本。因此,我认为缺少的链接是您应该将构建的字符串分配给命令文本

oCommand.CommandText = sSQL.ToString()

然后在其后添加参数

Private Function GetSvcType(ByVal oCommand As OleDbCommand, ByVal SSTGroupID As Integer) As DataTable
    Dim sSQL As New StringBuilder()
    sSQL.AppendLine(" Select SSTServiceTypeID AS ID, SSTServiceTypeName AS Name ")
    sSQL.AppendLine(" from fgen_SSTServiceType (nolock) ")
    sSQL.AppendLine(" Where 1=1 AND Disabled = 0 ")
    sSQL.AppendLine(" AND fgen_SSTServiceType.SSTGroupID = @SSTGroupID ")
    oCommand.CommandText = sSQL.ToString()
    oCommand.Parameters.AddWithValue("@SSTGroupID", SSTGroupID)
    Return GetDataTable(oCommand.CommandText)
End Function

或者,您可能想使用Using创建并处理命令。我会写的,但是看不到您的联系,因此您应该以this answer为例。

答案 1 :(得分:0)

我不确定Where 1 = 1(no lock)在做什么,因此我将其删除。

函数FillDataTable包含您所有的数据库访问代码,这使它与用户界面代码分开。数据库对象应为区域设置,以便您可以控制它们是否已关闭和处置。 Using...End Using块即使有错误也可以解决此问题。摆脱任何用于命令和连接的类级变量。命令和连接都包括在内;如果使用的第一行,请注意末尾的逗号。

您可以将连接字符串直接传递给连接的构造函数,并将命令文本和连接直接传递给命令的构造函数。无需单独设置这些属性。

OleDb不注意参数的名称,因此,将参数添加到Parameters集合的顺序必须与参数在命令文本中出现的顺序匹配。在这种情况下,您只有一个,仅供以后参考。最好使用包含数据库数据类型的Parameters.Add()。参见http://www.dbdelta.com/addwithvalue-is-evil/https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ 还有一个: https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications 这是另一个 https://andrevdm.blogspot.com/2010/12/parameterised-queriesdont-use.html

注意:我不得不猜测您参数的数据类型。检查数据库中的实际类型。

始终在最后一个时刻(.Execute...之前的行)打开连接,并尽快关闭连接(End Using

Private Function FillDataTable(GroupID As Long) As DataTable
    Dim strSQL = "Select SSTServiceTypeID AS ID, SSTServiceTypeName As Name
    From fgen_SSTServiceType 
    Where Disabled = 0
    And SSTGroupID = @SSTGroupID "
    Dim dt As New DataTable
    Using cn As New OleDbConnection("Your connection string"),
            cmd As New OleDbCommand(strSQL, cn)
        cmd.Parameters.Add("@SSTGroupID", OleDbType.BigInt).Value = GroupID
        cn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    Return dt
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt = FillDataTable(7L) 'the L indicates that this is a long,pass the GroupID to the function
    DataGridView1.DataSource = dt
End Sub

编辑

Dim dt = FillDataTable(7L) 'In the button code

然后在数据访问代码中将Oledb更改为Sql

Imports System.Data.SqlClient
Class DataAccess
    Private Function FillDataTable(GroupID As Long) As DataTable
        Dim strSQL = "Select SSTServiceTypeID AS ID, SSTServiceTypeName As Name
From fgen_SSTServiceType 
Where Disabled = 0
And SSTGroupID = @SSTGroupID "
        Dim dt As New DataTable
        Using cn As New SqlConnection("Your connection string"),
        cmd As New SqlCommand(strSQL, cn)
            cmd.Parameters.Add("@SSTGroupID", SqlDbType.BigInt).Value = GroupID
            cn.Open()
            dt.Load(cmd.ExecuteReader)
        End Using
        Return dt
    End Function
End Class