示例#1
0
 public void ExecuteNonQueryTest1()
 {
     using (SqlServerTransaction tranObj = SqlHelper.BeginTranscation())
     {
         try
         {
             string _sql = "insert into [Person](PName,PAge,PAddress) values(@pname,@page,@paddress)";
             SqlHelper.ExecuteNonQuery(tranObj, _sql,
                                       new DbParameter[3]
             {
                 new SqlParameter("@pname", "YanZhiwei"),
                 new SqlParameter("@page", 18),
                 new SqlParameter("@paddress", "zhuzhou")
             });
             throw new System.Exception("test");
             SqlHelper.ExecuteNonQuery(tranObj, _sql,
                                       new DbParameter[3]
             {
                 new SqlParameter("@pname", "YanZhiwei2"),
                 new SqlParameter("@page", 19),
                 new SqlParameter("@paddress", "zhuzhou2")
             });
             tranObj.CommitTransaction();
         }
         catch (System.Exception ex)
         {
             tranObj.RollbackTransaction();
         }
     }
 }
        /// <summary>
        /// 创建角色权限
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <param name="roleCode">角色代码</param>
        /// <param name="modulePermissionId">模块操作标识ID数组</param>
        /// <returns>
        /// 创建是否成功
        /// </returns>
        /// 时间:2016-04-29 10:21
        /// 备注:
        public bool CreateRole(string roleName, string roleCode, string[] modulePermissionId)
        {
            ValidateOperator.Begin().NotNullOrEmpty(roleName, "角色名称").NotNullOrEmpty(roleCode, "角色代码").NotNull(modulePermissionId, "角色对应模块操作标识ID数组");
            bool _result = false;

            using (SqlServerTransaction sqlTran = sqlHelper.BeginTranscation())
            {
                try
                {
                    SqlParameter[] _paramter = new SqlParameter[2];
                    _paramter[0] = new SqlParameter("@roleCode", roleCode);
                    _paramter[1] = new SqlParameter("@roleName", roleName);
                    sqlHelper.ExecuteNonQuery(sqlTran, "insert into Roles(R_Code,R_Name) values(@roleCode,@roleName)", _paramter);

                    foreach (string s in modulePermissionId)
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            _paramter    = new SqlParameter[2];
                            _paramter[0] = new SqlParameter("@roleCode", roleCode);
                            _paramter[1] = new SqlParameter("@mpId", s);
                            sqlHelper.ExecuteNonQuery(sqlTran, "insert into RolePermissions values(@roleCode,@mpId)", _paramter);
                        }
                    }

                    sqlTran.CommitTransaction();
                    _result = true;
                }
                catch (Exception)
                {
                    sqlTran.RollbackTransaction();
                    _result = false;
                }

                return(_result);
            }
        }