欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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();
}
}