基于SQL Server 的Exception Handling - PART II
2008-12-06 10:15:28 来源:WEB开发网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>());
}
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
- ››SQL SERVER无法安装成功,sqlstp.log文件提示[未发...
- ››Sql Server中通过父记录查找出所有关联的子记录
- ››基于IP地址的vsftp服务器
- ››SqlServer触发器、存储过程和函数
- ››SQL Server 中的事务(含义,属性,管理)
- ››Sqlite数据库插入和读取图片数据
- ››Sql server 2005拒绝了对对象 'xx表' (数...
更多精彩
赞助商链接