错误:必须声明标量变量"@data"

Pri*_*bin 5 asp.net

当执行到达时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();
Run Code Online (Sandbox Code Playgroud)

m1k*_*ael 4

代替

string commandText = "update groups set subjectline ='" +    txtSubjectLine.Text + "',data= @data where groupid = " + ddlGroup.SelectedItem.Value + " ";
Run Code Online (Sandbox Code Playgroud)

string commandText = "update groups set subjectline ='" +    txtSubjectLine.Text + "',data= ? where groupid = " + ddlGroup.SelectedItem.Value + " ";
Run Code Online (Sandbox Code Playgroud)

即,将“@data”替换为“?” 在命令文本中。这是使用 OleDbCommand 指定参数占位符的方式。


以下是编辑后的原文:

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();
Run Code Online (Sandbox Code Playgroud)