public int update(实体类名称 entity)
{
//数据库链接字符串
private string sqlconn = "server=数据库地址;database=数据库名称;user=登录名;pwd=密码;";
int updateRows = 0;//返回影响行数
SqlConnection conn = new SqlConnection();
conn.ConnectionString = sqlconn;
//update、inset、delete语句
string sql2 = "UPDATE 表名 SET 字段名1=@field1, 字段名2=@field2 WHERE 字段名3=@field3";
SqlCommand myCommand2 = new SqlCommand(sql2, conn);//
//参数化传参
myCommand2.Parameters.AddWithValue("@field1", entity.对应字段名1);
myCommand2.Parameters.AddWithValue("@field2", entity.对应字段名2);
myCommand2.Parameters.AddWithValue("@field3", entity.对应字段名3);
try
{
conn.Open();
updateRows = myCommand2.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex);
}
finally
{
conn.Close();
}
return updateRows;
}
10-24
2684
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)