在C#代码文件中,使用存储过程,怎样抛出错误信息和终止存储过程,代码如下:
//更改用户邮箱publicvoidModifyEmail(intUserId,stringNewEmail,stringOldEmail){stringEmail=""...
//更改用户邮箱
public void ModifyEmail(int UserId, string NewEmail,string OldEmail)
{
string Email = "";
string cmdText = "select Email from Users TABLOCKX where UserId="+UserId;
SqlConnection conn = new SqlConnection(Connection.ConnString);
SqlCommand cmd = new SqlCommand(cmdText, conn);
conn.Open();
SqlTransaction tr = conn.BeginTransaction();
cmd.Transaction = tr;
try
{
SqlDataReader dr = cmd.ExecuteReader(); //要求此处能抛出错误
if (dr.Read())
{
Email = dr["Email"].ToString();
}
dr.Close();
if (Email == OldEmail)
{
cmd.CommandText = "Update users Set Email='" + NewEmail + "' Where UserId=" + UserId;
cmd.ExecuteNonQuery();
}
else
{
//要求此处能抛出错误,或中止执行存储过程(此处能中止存储过程执行吗?)
}
tr.Commit();
}
catch(Exception ex)
{
tr.Rollback();
throw ex;
}
finally { conn.Close(); }
} 展开
public void ModifyEmail(int UserId, string NewEmail,string OldEmail)
{
string Email = "";
string cmdText = "select Email from Users TABLOCKX where UserId="+UserId;
SqlConnection conn = new SqlConnection(Connection.ConnString);
SqlCommand cmd = new SqlCommand(cmdText, conn);
conn.Open();
SqlTransaction tr = conn.BeginTransaction();
cmd.Transaction = tr;
try
{
SqlDataReader dr = cmd.ExecuteReader(); //要求此处能抛出错误
if (dr.Read())
{
Email = dr["Email"].ToString();
}
dr.Close();
if (Email == OldEmail)
{
cmd.CommandText = "Update users Set Email='" + NewEmail + "' Where UserId=" + UserId;
cmd.ExecuteNonQuery();
}
else
{
//要求此处能抛出错误,或中止执行存储过程(此处能中止存储过程执行吗?)
}
tr.Commit();
}
catch(Exception ex)
{
tr.Rollback();
throw ex;
}
finally { conn.Close(); }
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询