事务处理拼接sql语句对数据库的操作.异常回滚
时间:2021-07-01 10:21:17
帮助过:2人阅读
List<UserBaseInfoModel>
GetCityByExamId(Guid ExamID)
{
var cur_user =
this.GetCurrentUser();
using (SqlConnection conn =
new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd;
using (SqlTransaction trans =
conn.BeginTransaction())
{
try
{
string sql =
@"
SELECT u.City FROM Exam_User_Power p,dbo.User_BaseInfo u
WHERE p.ExamID=@ExamID
AND p.UserID=u.UserID
GROUP BY u.City
";
cmd =
new SqlCommand(sql, conn, trans);
cmd.Parameters.AddWithValue("ExamID", ExamID);
SqlDataAdapter sda =
new SqlDataAdapter(cmd);
DataSet ds =
new DataSet();
sda.Fill(ds);
List<UserBaseInfoModel> list = (List<UserBaseInfoModel>)IListDataSet.DataSetToIList<UserBaseInfoModel>(ds,
0);
trans.Commit();
return list;
}
catch
{
trans.Rollback();
throw;
}
finally
{
conn.Close();
}
}
}
return null;
}
事务处理拼接sql语句对数据库的操作.异常回滚
标签: