Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 必须声明标量变量"@SSTGroupID"; 私有函数GetSvcType(ByVal oCommand作为OleDbCommand,ByVal SSTGroupID作为Integer)作为数据表 将sSQL设置为新的StringBuilder AppendLine(“选择SSTServiceTypeID作为ID,选择SSTServiceTypeName作为名称”) sSQL.AppendLine(“来自fgen_SSTServiceType(nolock)”) sSQL.AppendLine(“其中1=1,Disabled=0”) sSQL.AppendLine(“和fgen_SSTServiceType.SSTGroupID=@SSTGroupID”) oCommand.Parameters.AddWithValue(“@SSTGroupID”,SSTGroupID) 返回GetDataTable(sSQL.ToString) 端函数_Vb.net_Visual Studio_Scalar - Fatal编程技术网

Vb.net 必须声明标量变量"@SSTGroupID"; 私有函数GetSvcType(ByVal oCommand作为OleDbCommand,ByVal SSTGroupID作为Integer)作为数据表 将sSQL设置为新的StringBuilder AppendLine(“选择SSTServiceTypeID作为ID,选择SSTServiceTypeName作为名称”) sSQL.AppendLine(“来自fgen_SSTServiceType(nolock)”) sSQL.AppendLine(“其中1=1,Disabled=0”) sSQL.AppendLine(“和fgen_SSTServiceType.SSTGroupID=@SSTGroupID”) oCommand.Parameters.AddWithValue(“@SSTGroupID”,SSTGroupID) 返回GetDataTable(sSQL.ToString) 端函数

Vb.net 必须声明标量变量"@SSTGroupID"; 私有函数GetSvcType(ByVal oCommand作为OleDbCommand,ByVal SSTGroupID作为Integer)作为数据表 将sSQL设置为新的StringBuilder AppendLine(“选择SSTServiceTypeID作为ID,选择SSTServiceTypeName作为名称”) sSQL.AppendLine(“来自fgen_SSTServiceType(nolock)”) sSQL.AppendLine(“其中1=1,Disabled=0”) sSQL.AppendLine(“和fgen_SSTServiceType.SSTGroupID=@SSTGroupID”) oCommand.Parameters.AddWithValue(“@SSTGroupID”,SSTGroupID) 返回GetDataTable(sSQL.ToString) 端函数,vb.net,visual-studio,scalar,Vb.net,Visual Studio,Scalar,我连续搜索了几个小时,似乎找不到解决办法。我需要你的帮助,谢谢 我已经更新了我的问题,包括GetDataTable函数。请看一看,谢谢。您的命令从未从StringBuilder获取文本。因此,我认为缺少的链接是,您应该将构建的字符串指定给命令文本 oCommand.CommandText = sSQL.ToString() 然后在后面添加参数 私有函数GetSvcType(ByVal oCommand作为OleDbCommand,ByVal SSTGroupID作为Integer)作为数据表

我连续搜索了几个小时,似乎找不到解决办法。我需要你的帮助,谢谢


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

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

oCommand.CommandText = sSQL.ToString()
然后在后面添加参数

私有函数GetSvcType(ByVal oCommand作为OleDbCommand,ByVal SSTGroupID作为Integer)作为数据表
将sSQL作为新的StringBuilder()进行调整
AppendLine(“选择SSTServiceTypeID作为ID,选择SSTServiceTypeName作为名称”)
sSQL.AppendLine(“来自fgen_SSTServiceType(nolock)”)
sSQL.AppendLine(“其中1=1,Disabled=0”)
sSQL.AppendLine(“和fgen_SSTServiceType.SSTGroupID=@SSTGroupID”)
oCommand.CommandText=sSQL.ToString()
oCommand.Parameters.AddWithValue(“@SSTGroupID”,SSTGroupID)
返回GetDataTable(oCommand.CommandText)
端函数

或者,您可能希望使用
Using
创建命令并对其进行处理。我会写的,但我看不到你的连接,所以你应该查看一个例子。

我不确定
中的1=1
(无锁)
在做什么,所以我删除了它们

函数
FillDataTable
包含所有数据库访问代码,使其与用户界面代码分开。您的数据库对象应该是区域设置的,这样您就可以控制它们的关闭和释放。即使出现错误,
Using…End Using
块也会处理此问题。去掉命令和连接的任何类级变量。包括命令和连接;如果第一行使用,请注意结尾处的逗号

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

OleDb不注意参数的名称,因此,参数添加到参数集合的顺序必须与参数在命令文本中的显示顺序相匹配。在这种情况下,您只有一个,但仅供将来参考。最好使用包含数据库数据类型的
参数.Add()
。看见 和 还有一个: 这是另一个

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

始终在最后一刻打开连接(在
.Execute…
之前的一行),并尽快关闭连接(使用
结束

编辑

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

您提供的代码中没有任何参数被添加到您实际执行的同一个命令中。这是因为AppendLine不支持addWithValues的变量,这恐怕毫无意义。这两种方法互不相干。您只需使用
StringBuilder
构建一个包含SQL代码的
字符串。然后,您必须将该
字符串
指定给命令对象的
CommandText
,并且需要将参数添加到同一命令中。您提供的代码中没有具体的证据表明您正在这样做。那么,我如何将我的
addwithvalue
包含到
getdatatable
方法中?如果我们从未见过
getdatatable
方法中的内容,我们怎么知道呢?我遵循了您的方法,但似乎仍然得到相同的错误。我发现它与我的
GetDataTable
方法有关,并且没有将
@SSTGroupID
包含在这个方法中。我已把这个方法列入问题中供你参考。错误指向
oDataAdapter.Fill(oDataTable)
SSTGroupID
ServiceTypeID
都是
Long
类型。
SSTServiceTypeName
的类型为String@JiunnWyeLeow请看我的答案的变化
GroupID只要长
OleDbType.BigInt
当我使用您的函数时,我在
dt.Load(cmd.ExecuteReader)
什么版本的Access和什么版本的Visual Studio?版本Visual Studio Professional 2015 14.0.25431.01更新3中得到相同的错误。我没有使用Access,我使用的是另一种db软件
Dim dt = FillDataTable(7L) 'In the button code
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