当执行到达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();
解决方法
更换
string commandText = "update groups set subjectline ='" + txtSubjectLine.Text + "',data= @data where groupid = " + ddlGroup.SelectedItem.Value + " ";
同
string commandText = "update groups set subjectline ='" + txtSubjectLine.Text + "',data= ? where groupid = " + ddlGroup.SelectedItem.Value + " ";
也就是说,将“@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();