我在一个字符串中包含以下 SQL 字符串,用于从 textBox 更新表。
string actualizar = "update pelicula set nombre_pelicula = " + txtnombre.Text.Trim() + " , web_pelicula = " + txtweb.Text.Trim() + " , descripcion = " + txtdescripcion.Text.Trim() + " , anio = " + txtanio.Text.Trim() + "where id_pelicula = " + txtid.Text.Trim();
SqlCommand comando = new SqlCommand(actualizar, conexion);
SqlDataReader dr = comando.ExecuteReader();
//comando.ExecuteNonQuery();
if (dr.Read())
{
MessageBox.Show("Se actualizo correctamente");
conexion.Close();
}
但是在修改元素时出现以下错误:
其中第 84 行是:
SqlDataReader dr = comando.ExecuteReader();
另外,当我将此行替换为:
comando.ExecuteNonQuery();
继续报错
您必须使用参数,将字符串中的值连接起来是一种不好的做法
它也
update
用于ExecuteNonQuery()
阅读器仅适用于select
在表中类型为 varchar 或 nvarchar 或 text 的字段中,执行查询时,必须将其括在单引号或后缀“”中。
查询中的任何文本都必须包含“”。
尝试如下: