ADO.NET删除数据
删除数据的过程和插入数据的过程基本相同,只要改变Sql语句即可。给“删除”按钮的Click事件添加代码如下:
private void btnDelete_Click(object sender, EventArgs e)
{
string strcon = @"data source=.\sqlexpress;initial catalog=School;uid=sa;pwd=123456";
SqlConnection con = new SqlConnection(strcon);
string sql = "delete from Student where id="+Convert.ToInt32(this.txtId.Text)+"";
SqlCommand comm = new SqlCommand(sql, con);
try
{
con.Open();
if (comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
浙公网安备 33010602011771号