WEB开发网
开发学院数据库MSSQL Server 基于SQL Server 的Exception Handling - PART II 阅读

基于SQL Server 的Exception Handling - PART II

 2008-12-06 10:15:28 来源:WEB开发网   
核心提示: 5. Create User, Create Role, Add User In Role, Delete All Dataprivate const string ErrorUserExists = "This user is already existent";pr

5.  Create User, Create Role, Add User In Role, Delete All Data

    private const string ErrorUserExists = "This user is already existent";
    private const string ErrorRoleExists = "This role is already existent";
    private const string ErrorUserNotExists = "This user does not exist";
    private const string ErrorRoleNotExists = "This role does not exist";
    private const string ErrorUserInRole = "This user is already in the role";
  
    public static bool CreateUser(string userName)
    {
      string procedureName = "P_USERS_I";
      Dictionary<string, object> parameters = new Dictionary<string, object>();
      parameters.Add("user_id", Guid.NewGuid().ToString());
      parameters.Add("user_name", userName);
      try
      {
        ExecuteCommand(procedureName, parameters);
        return true;
      }
      catch (Exception ex)
      {
        if (ex.Message == ErrorUserExists)
        {
          Console.WriteLine("The user "{0}" you specify is already existent!",userName);
          return false; ;
        }
        Console.WriteLine("A unhandled exception is thrown for some unknown reason!");
        return false;
      }
    }
  
    public static bool CreateRole(string roleName)
    {
      string procedureName = "P_ROLES_I";
      Dictionary<string, object> parameters = new Dictionary<string, object>();
      parameters.Add("role_id", Guid.NewGuid().ToString());
      parameters.Add("role_name", roleName);
      try
      {
        ExecuteCommand(procedureName, parameters);
        return true;
      }
      catch (Exception ex)
      {
        if (ex.Message==ErrorRoleExists)
        {
          Console.WriteLine("The role "{0}" you specify is already existent!",roleName);
          return false; ;
        }
        Console.WriteLine("A unhandled exception is thrown for some unknown reason!");
        return false;
      }
    }
  
    public static bool AddUserInRole(string userName, string roleName)
    {
      string procedureName = "P_USERS_IN_ROLES_I";
      Dictionary<string, object> parameters = new Dictionary<string, object>();
      parameters.Add("user_name", userName);
      parameters.Add("role_name", roleName);
      try
      {
        ExecuteCommand(procedureName, parameters);
        return true;
      }
      catch (Exception ex)
      {
        if (ex.Message==ErrorUserNotExists)
        {
          Console.WriteLine("The user "{0}" you specify is not existent!", userName);
          return false;
        }
        if (ex.Message==ErrorRoleNotExists)
        {
          Console.WriteLine("The role "{0}" you specify is not existent!",roleName);
          return false;
        }
        if (ex.Message == ErrorUserInRole)
        {
          Console.WriteLine("The user "{0}" is in the role "{1}"!",userName,roleName);
          return false;
        }
        Console.WriteLine("A unhandled exception is thrown for some unknown reason!");
        return false;
      }
}
 public static void Clear()
    {
      ExecuteCommand("P_CLEAR_DATA", new Dictionary<string, object>());
    }

上一页  3 4 5 6 7 8 9  下一页

Tags:基于 SQL Server

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接