事务处理拼接sql语句对数据库的操作.异常回滚

       public 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;
        }

posted @ 2016-04-07 10:41  团子先生  阅读(284)  评论(0编辑  收藏  举报