I am getting an error when execution reaches cmd.ExecuteNonQuery()
which says Must declare the scalar variable:
当执行到达cmd.ExecuteNonQuery()时,我收到一个错误,其中说必须声明标量变量:
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = Connection;
cmd.CommandTimeout = 0;
string commandText = "update groups set subjectline ='" + txtSubjectLine.Text + "',data= @data where groupid = " + ddlGroup.SelectedItem.Value + " ";
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@Data",OleDbType.VarBinary);
cmd.Parameters["@Data"].Value = binarydata;
cmd.ExecuteNonQuery();
2 个解决方案
#1
4
Replace
更换
string commandText = "update groups set subjectline ='" + txtSubjectLine.Text + "',data= @data where groupid = " + ddlGroup.SelectedItem.Value + " ";
with
同
string commandText = "update groups set subjectline ='" + txtSubjectLine.Text + "',data= ? where groupid = " + ddlGroup.SelectedItem.Value + " ";
that is, replace "@data" with "?" in the command text. This is how you specify parameter placeholders with OleDbCommand.
也就是说,将“@data”替换为“?”在命令文本中。这是使用OleDbCommand指定参数占位符的方法。
Here's the edited original:
这是编辑过的原文:
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = Connection;
cmd.CommandTimeout = 0;
cmd.CommandText = "update groups set subjectline ='" + txtSubjectLine.Text + "', data = ? where groupid = " + ddlGroupSelectedItem.Value;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("p1", OleDbType.VarBinary);
cmd.Parameters["p1"].Value = binarydata;
cmd.ExecuteNonQuery();
#2
0
This link shows accepted answer on how to update binarydata to table using .Write
此链接显示如何使用.Write将binarydata更新为表的已接受答案
http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/dc1b053d-f0d5-48f8-ad82-fb6d96d27f80
If that not solves your problem go ahead and read below:
如果这不能解决您的问题,请继续阅读以下内容:
You have used variable @data whereas you are declaring variable @Data, both are of different case.
你使用了变量@data而你声明变量@Data,两者都是不同的情况。
In certain condition variable names can be case sensitive in TSQL, for instance if MS Sql server is installed using case sensitive collation, then table, column, variable names become case sensitive, even if database has case insensitive collation.
在某些情况下,变量名称在TSQL中可以区分大小写,例如,如果使用区分大小写的排序规则安装MS Sql server,则表,列,变量名称将区分大小写,即使数据库具有不区分大小写的排序规则。
see the following links for more details.
有关详细信息,请参阅以下链接。
Case sensitive variables in SQL Server
SQL Server中的区分大小写的变量
SQL语法区分大小写吗?
#1
4
Replace
更换
string commandText = "update groups set subjectline ='" + txtSubjectLine.Text + "',data= @data where groupid = " + ddlGroup.SelectedItem.Value + " ";
with
同
string commandText = "update groups set subjectline ='" + txtSubjectLine.Text + "',data= ? where groupid = " + ddlGroup.SelectedItem.Value + " ";
that is, replace "@data" with "?" in the command text. This is how you specify parameter placeholders with OleDbCommand.
也就是说,将“@data”替换为“?”在命令文本中。这是使用OleDbCommand指定参数占位符的方法。
Here's the edited original:
这是编辑过的原文:
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = Connection;
cmd.CommandTimeout = 0;
cmd.CommandText = "update groups set subjectline ='" + txtSubjectLine.Text + "', data = ? where groupid = " + ddlGroupSelectedItem.Value;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("p1", OleDbType.VarBinary);
cmd.Parameters["p1"].Value = binarydata;
cmd.ExecuteNonQuery();
#2
0
This link shows accepted answer on how to update binarydata to table using .Write
此链接显示如何使用.Write将binarydata更新为表的已接受答案
http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/dc1b053d-f0d5-48f8-ad82-fb6d96d27f80
If that not solves your problem go ahead and read below:
如果这不能解决您的问题,请继续阅读以下内容:
You have used variable @data whereas you are declaring variable @Data, both are of different case.
你使用了变量@data而你声明变量@Data,两者都是不同的情况。
In certain condition variable names can be case sensitive in TSQL, for instance if MS Sql server is installed using case sensitive collation, then table, column, variable names become case sensitive, even if database has case insensitive collation.
在某些情况下,变量名称在TSQL中可以区分大小写,例如,如果使用区分大小写的排序规则安装MS Sql server,则表,列,变量名称将区分大小写,即使数据库具有不区分大小写的排序规则。
see the following links for more details.
有关详细信息,请参阅以下链接。
Case sensitive variables in SQL Server
SQL Server中的区分大小写的变量
SQL语法区分大小写吗?