Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL语句在C中失败#_C#_Sql_Sql Server - Fatal编程技术网

C# SQL语句在C中失败#

C# SQL语句在C中失败#,c#,sql,sql-server,C#,Sql,Sql Server,我正在尝试执行以下代码: SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB rest of my connstring.... "); SqlCommand update = new SqlCommand("Update tblImages SET Name=@name, Descript=@descript WHERE Id=@id", connection); update.Parameters.Add(

我正在尝试执行以下代码:

SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB  rest of my connstring.... ");
SqlCommand update = new SqlCommand("Update tblImages SET Name=@name, Descript=@descript WHERE Id=@id", connection);
update.Parameters.Add("name", TextBox3.Text);
update.Parameters.Add("descript",TextBox4.Text);
update.Parameters.Add("id",id);
update.ExecuteNonQuery();

执行
ExecuteNonQuery()

时出错,您需要在
更新.ExecuteNonQuery()之前执行
连接.Open()
。您还需要使用确保在执行
update.ExecuteNonQuery()
后关闭
连接。
update.Parameters.Add(…)
方法中的参数名称也错误,您忘记添加
@

using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB  rest of my connstring.... "))
{
    using (SqlCommand update = new SqlCommand("Update tblImages SET Name = @name, Descript = @descript WHERE Id = @id", connection))
    {
        update.Parameters.Add(new SqlParameter("@name", TextBox3.Text));
        update.Parameters.Add(new SqlParameter("@descript", TextBox4.Text));
        update.Parameters.Add(new SqlParameter("@id", id));

        connection.Open();
        update.ExecuteNonQuery();
    }
}

我认为这里的问题是在add-parameter语句中缺少“@”符号。您还使用了add而不是
AddWithValue

尝试将其更改为:

SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB  rest of my connstring.... ");
connection.Open();
SqlCommand update = new SqlCommand("Update tblImages SET Name=@name, Descript=@descript WHERE Id=@id", connection);
update.Parameters.AddWithValue("@name", TextBox3.Text);
update.Parameters.ADdWithValue("@descript", TextBox4.Text);
update.Parameters.AddWithValue("@id", id);
update.ExecuteNonQuery();
请注意,我还更改了代码以打开您创建的连接。

您遇到了什么错误?