/// <summary> /// 根據操做員名稱和密碼獲取操做員實體 /// </summary> /// <param name="name">操做員名稱</param> /// <param name="pwd">操做員密碼</param> /// <returns>操做員實體</returns> public Model.Operator GetOperatorInfoByName(string name, string pwd) { //SQL命令 string sqltxt = string.Format( "Select Id, OperatorName, Password, RightsList, State From Operator Where OperatorName = '{0}' And Password = '{1}'" , name, pwd); //建立操做員實體 Model.Operator tmpOperator = new Model.Operator(); // 轉換數據庫存儲的 二進制數據爲 Byte[] 數組 以便進而轉換爲操做員權限集合 // 從配置文件讀取鏈接字符串 string connectionString = ConfigurationManager.ConnectionStrings[ "SQLSERVER" ].ConnectionString; // 執行 SQL 命令 using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(sqltxt, conn); conn.Open(); using (SqlDataReader myReader = cmd.ExecuteReader( CommandBehavior.CloseConnection | CommandBehavior.SingleResult | CommandBehavior.SingleRow)) { if (myReader.Read()) { //將數據集轉換成實體集合 tmpOperator.Id = Convert.ToInt32(myReader[ "Id" ]); tmpOperator.ModelName = Convert.ToString(myReader[ "OperatorName" ]); tmpOperator.Password = Convert.ToString(myReader[ "Password" ]); tmpOperator.State = Convert.ToBoolean(myReader[ "State" ]); // 讀取權限集合 System.Data.SqlTypes.SqlBytes bytes = myReader.GetSqlBytes( 3 ); // 只能指定列序號 // 將流反序列化爲權限集合對象 BinaryFormatter bf = new BinaryFormatter(); if (!bytes.IsNull) tmpOperator.RightsCollection = (bf.Deserialize(bytes.Stream) as Dictionary<string, Model.Rights>); //else // throw new Exception(string.Format("操做員 [{0}] 沒有任何權限,禁止登陸!", tmpOperator.ModelName)); } else //若是沒有讀取到內容則拋出異常 throw new Exception( "登陸名稱或用戶密碼不正確!" ); } } // 若是操做員已經被禁用 if (!tmpOperator.State) throw new Exception(string.Format( "操做員 [{0}] 已被禁用,請與管理員聯繫!" , tmpOperator.ModelName)); // 返回結果 return tmpOperator; } /// <summary> /// 添加操做員 /// </summary> /// <param name="addOperator">要添加的操做員實體</param> /// <returns>True:成功/False:失敗</returns> public bool AddOperator(Model.Operator addOperator) { // 驗證密碼長度 if (addOperator.Password.Trim().Length < 6 ) throw new Exception( "用戶密碼長度不能小於六位!" ); // 轉換操做員權限集合爲數據庫可存取的 Byte[] 數組 MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, addOperator.RightsCollection); byte [] rigthsByteArray = new byte [( int )(ms.Length)]; ms.Position = 0 ; ms.Read(rigthsByteArray, 0 , ( int )(ms.Length)); ms.Close(); // 拼接 SQL 命令 string sqlTxt = "Insert Into Operator (OperatorName, Password, RightsList, State) Values " "(@OperatorName, @Password, @RightsList, @State)" ; // 從配置文件讀取鏈接字符串 string connectionString = ConfigurationManager.ConnectionStrings[ "SQLSERVER" ].ConnectionString; // 執行 SQL 命令 using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(sqlTxt, conn); SqlParameter prm1 = new SqlParameter( "@OperatorName" , SqlDbType.NVarChar, 50 ); prm1.Value = addOperator.ModelName; SqlParameter prm2 = new SqlParameter( "@Password" , SqlDbType.NVarChar, 50 ); prm2.Value = addOperator.Password; SqlParameter prm3 = new SqlParameter( "@RightsList" , SqlDbType.VarBinary, rigthsByteArray.Length, ParameterDirection.Input, false , 0 , 0 , null , DataRowVersion.Current, rigthsByteArray); SqlParameter prm4 = new SqlParameter( "@State" , SqlDbType.Bit); prm4.Value = addOperator.State; cmd.Parameters.AddRange( new SqlParameter[] { prm1, prm2, prm3, prm4 }); conn.Open(); if (cmd.ExecuteNonQuery() >= 1 ) return true ; else return false ; } } /// <summary> /// 刪除操做員 /// </summary> /// <param name="id">要刪除的操做員 ID</param> /// <returns>True:成功/False:失敗</returns> public bool DeleteOperatorByID( int id) { // 刪除單個信息 SQL 命令 string sqlTxt = string.Format( "Delete From Operator Where Id = {0}" , id); // 建立 SQL 執行對象 DBUtility.AbstractDBProvider dbProvider = DBUtility.AbstractDBProvider.Instance(); // 執行 刪除操做 int rowsAffected; dbProvider.RunCommand(sqlTxt, out rowsAffected); if (rowsAffected >= 1 ) return true ; else return false ; } /// <summary> /// 修改操做員 /// </summary> /// <param name="currentOperator">要修改的操做員實體</param> /// <returns>True:成功/False:失敗</returns> public bool ModifyOperator(Model.Operator currentOperator) { // 驗證密碼長度 if (currentOperator.Password.Trim().Length < 6 ) throw new Exception( "用戶密碼長度不能小於六位!" ); // 轉換操做員權限集合爲數據庫可存取的 Byte[] 數組 MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, currentOperator.RightsCollection); byte [] rigthsByteArray = new byte [( int )(ms.Length)]; ms.Position = 0 ; ms.Read(rigthsByteArray, 0 , ( int )(ms.Length)); ms.Close(); // 拼接 SQL 命令 string sqlTxt = "Update Operator Set OperatorName = @OperatorName, " "Password = @Password, RightsList = @RightsList, State = @State Where Id = @Id" ; // 從配置文件讀取鏈接字符串 string connectionString = ConfigurationManager.ConnectionStrings[ "SQLSERVER" ].ConnectionString; // 執行 SQL 命令 using(SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(sqlTxt, conn); SqlParameter prm1 = new SqlParameter( "@OperatorName" , SqlDbType.NVarChar, 50 ); prm1.Value = currentOperator.ModelName; SqlParameter prm2 = new SqlParameter( "@Password" , SqlDbType.NVarChar, 50 ); prm2.Value = currentOperator.Password; SqlParameter prm3 = new SqlParameter( "@RightsList" , SqlDbType.VarBinary, rigthsByteArray.Length, ParameterDirection.Input, false , 0 , 0 , null , DataRowVersion.Current, rigthsByteArray); SqlParameter prm4 = new SqlParameter( "@State" , SqlDbType.Bit); prm4.Value = currentOperator.State; SqlParameter prm5 = new SqlParameter( "@Id" , SqlDbType.Int); prm5.Value = currentOperator.Id; cmd.Parameters.AddRange( new SqlParameter[] { prm1, prm2, prm3, prm4, prm5 }); conn.Open(); if (cmd.ExecuteNonQuery() >= 1 ) return true ; else return false ; } } /// <summary> /// 獲取全部操做員信息 /// </summary> /// <returns>操做員實體集合</returns> public Dictionary<string, Model.Operator> GetAllOperatorInfo() { //SQL命令 string sqltxt = "Select Id, OperatorName, Password, RightsList, State From Operator" ; //建立操做員實體集合 Dictionary<string, Model.Operator> operatorCollection = new Dictionary<string, Model.Operator>(); //定義操做員實體 Model.Operator tmpOperator = null ; // 轉換數據庫存儲的 二進制數據爲 Byte[] 數組 以便進而轉換爲操做員權限集合 // 從配置文件讀取鏈接字符串 string connectionString = ConfigurationManager.ConnectionStrings[ "SQLSERVER" ].ConnectionString; // 執行 SQL 命令 using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(sqltxt, conn); conn.Open(); using (SqlDataReader myReader = cmd.ExecuteReader( CommandBehavior.CloseConnection)) { while (myReader.Read()) { // 建立操做員實體 tmpOperator = new Model.Operator(); //將數據集轉換成實體集合 tmpOperator.Id = Convert.ToInt32(myReader[ "Id" ]); tmpOperator.ModelName = Convert.ToString(myReader[ "OperatorName" ]); tmpOperator.Password = Convert.ToString(myReader[ "Password" ]); tmpOperator.State = Convert.ToBoolean(myReader[ "State" ]); // 讀取權限集合 System.Data.SqlTypes.SqlBytes bytes = myReader.GetSqlBytes( 3 ); // 只能指定列序號 // 將流反序列化爲權限集合對象 BinaryFormatter bf = new BinaryFormatter(); if (!bytes.IsNull) tmpOperator.RightsCollection = (bf.Deserialize(bytes.Stream) as Dictionary<string, Model.Rights>); // 添加到操做員實體集合 operatorCollection.Add(tmpOperator.ModelName, tmpOperator); } } } // 返回結果 return operatorCollection; } /// <summary> /// 根據操做員名稱校驗操做員是否存在 /// </summary> /// <param name="operatorName">操做員名稱</param> /// <returns>True:存在/Flase:不存在</returns> public bool CheckOperatorExist(string operatorName) { //建立查詢信息的 SQL string sqlTxt = string.Format( "Select Count(*) From Operator Where OperatorName = '{0}'" , operatorName); //建立SQL執行對象 DBUtility.AbstractDBProvider dbProvider = DBUtility.AbstractDBProvider.Instance(); //執行查詢操做 int result = Convert.ToInt32(dbProvider.RunCommand(sqlTxt)); if (result >= 1 ) return true ; else return false ; } #endregion |