ASP.Net简单更新方法举例
程序员文章站
2022-07-02 18:58:42
public int Update()
{
string sql = "update student set xingming=@xingming wher...
public int Update()
{
string sql = "update student set xingming=@xingming where xuehao=@xuehao";
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
SqlCommand command = new SqlCommand(sql,conn);
SqlParameter parm = new SqlParameter("@xuehao","001");
command.Parameters.Add(parm);
parm = new SqlParameter("@xingming", "张三");
command.Parameters.Add(parm);
try
{
conn.Open();
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
return -1;
}
finally
{
conn.Close();
}
}
{
string sql = "update student set xingming=@xingming where xuehao=@xuehao";
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
SqlCommand command = new SqlCommand(sql,conn);
SqlParameter parm = new SqlParameter("@xuehao","001");
command.Parameters.Add(parm);
parm = new SqlParameter("@xingming", "张三");
command.Parameters.Add(parm);
try
{
conn.Open();
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
return -1;
}
finally
{
conn.Close();
}
}