例程:
bool flag = false;
SqlConnection con = new SqlConnection(Constr);
con.Open();
SqlCommand cmd = new SqlCommand(cmdtxt,con);
try
{
//执行SQL语句
cmd.ExecuteNonQuery();
flag = true;
}
catch
{
flag = false;
}
finally
{
//释放资源
cmd.Dispose();
con.Close();
}
if (flag)
{
Response.Write("<script language=javascript>alert('添加成功!');location='Default.aspx'</script>");
}
else
{
Response.Write("<script language=javascript>alert('添加失败!');location='javascript:history.go(-1)'</script>");
}