C# 必须声明标量变量-SQL参数以填充DataGridView
我发现了类似的问题,但没有一个答案对我有帮助或适用于我 我正在使用VisualStudio2013、C#和名为T-SQL的数据库引擎 如果我用一个具体的值替换参数,SQL命令就可以正常工作。但我需要这里的参数 我得到这个错误: 必须声明标量变量“@Collection1” 在这一行:C# 必须声明标量变量-SQL参数以填充DataGridView,c#,datagridview,sqldataadapter,C#,Datagridview,Sqldataadapter,我发现了类似的问题,但没有一个答案对我有帮助或适用于我 我正在使用VisualStudio2013、C#和名为T-SQL的数据库引擎 如果我用一个具体的值替换参数,SQL命令就可以正常工作。但我需要这里的参数 我得到这个错误: 必须声明标量变量“@Collection1” 在这一行: myydata.Fill(myytab); 不管我做什么,@Collection1都不想被声明 string stringcommand = "select Name, Collection, Text from
myydata.Fill(myytab);
不管我做什么,@Collection1
都不想被声明
string stringcommand = "select Name, Collection, Text from Card_List where Collection IN" +
"(select Shortcut from Collections where Collection Like @Collection1)";
SqlConnection myConnection = new SqlConnection(stringconnection);
SqlCommand myCommand = new SqlCommand(stringcommand, myConnection);
// Tried to declare parameter 5 times here, unsuccessfully
myCommand.Parameters.Add(new SqlParameter("Collection1", string1));
myCommand.Parameters.Add("@Collection1", SqlDbType.NVarChar).Value = string1;
myCommand.Parameters["@Collection1"].Value = string1;
myCommand.Parameters.AddWithValue("@Collection1", string1);
SqlParameter Param1 = myCommand.Parameters.Add("@Collection1", SqlDbType.NVarChar);
Param1.Value = string1;
SqlDataAdapter myydata = new SqlDataAdapter();
myydata.SelectCommand = new SqlCommand(stringcommand, myConnection);
myConnection.Open();
DataTable myytab = new DataTable();
myydata.Fill(myytab);
BindingSource bsour = new BindingSource();
bsour.DataSource = myytab;
dataGridView1.DataSource = bsour;
myydata.Update(myytab);
myConnection.Close();
您希望设置的
SqlCommand myCommand
用作myydata。选择Command
,如:
myCommand.Parameters.Add(new SqlParameter("Collection1", string1));
myydata.SelectCommand = myCommand;
您使用myydata.SelectCommand=newSQLCommand(stringcommand,myConnection)的方式,该命令中有
@Collection1
,但是如果没有附加任何参数,则必须声明标量变量“@Collection1”。
错误。您正在添加多个参数!“你应该只添加一次。”我想OP只是展示了他尝试过的所有方法。我很确定他没有一次尝试所有的。哦!那么,在myydata.SelectCommand=newsqlcommand(stringcommand,myConnection)中使用的myCommand
在哪里代码>??所以SQL语句实际上没有传递任何参数。@JonBrave,你应该加上这一点作为回答,我认为你是对的,我看到了我的错误。非常感谢。