在網上看到別人的代碼,寫下來留著看,歡迎各位指教,有沒有工廠模式的學習資料啊,留下給小弟學習學習. 首先寫個Model類,實現業務實體,我的比較簡單,只有三個欄位 View Code 1 public class LoginModel 2 { 3 #region 4 5 private int _A ...
在網上看到別人的代碼,寫下來留著看,歡迎各位指教,有沒有工廠模式的學習資料啊,留下給小弟學習學習.
首先寫個Model類,實現業務實體,我的比較簡單,只有三個欄位
View Code1 public class LoginModel 2 { 3 #region 4 5 private int _AdminId; 6 private string _AdminUID; 7 private string _AdminPWD; 8 9 public string AdminPWD 10 { 11 get { return _AdminPWD; } 12 set { _AdminPWD = value; } 13 } 14 15 public int AdminId 16 { 17 get { return _AdminId; } 18 set { _AdminId = value; } 19 } 20 21 22 public string AdminUID 23 { 24 get { return _AdminUID; } 25 set { _AdminUID = value; } 26 } 27 28 #endregion 29 }
2.創建一個DBHelp類,實現SQLServer資料庫訪問
1 public class DBHelp 2 { 3 private static readonly string connectionString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString.ToString(); 4 private static readonly string providerName = "System.Data.SqlClient"; 5 6 /// <summary> 7 /// GetConnection 用於獲取連接資料庫的 connection 對象 8 /// </summary> 9 /// <returns></returns> 10 private static DbConnection GetConnection() 11 { 12 DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName); 13 DbConnection connection = _factory.CreateConnection(); 14 connection.ConnectionString = connectionString; 15 return connection; 16 } 17 18 /// <summary> 19 /// GetCommand 獲取命令參數 command 對象 20 /// </summary> 21 /// <param name="commandText"></param> 22 /// <param name="commandType"></param> 23 /// <param name="connection"></param> 24 /// <returns></returns> 25 private static DbCommand GetCommand(string commandText, CommandType commandType, DbConnection connection) 26 { 27 DbCommand command = connection.CreateCommand(); 28 command.CommandText = commandText; 29 command.CommandType = commandType; 30 return command; 31 } 32 33 /// <summary> 34 /// GetCommand 方法重載 35 /// </summary> 36 /// <param name="commandText">sql語句</param> 37 /// <param name="connection"></param> 38 /// <returns></returns> 39 private static DbCommand GetCommand(string commandText, DbConnection connection) 40 { 41 DbCommand command = connection.CreateCommand(); 42 command.CommandText = commandText; 43 command.CommandType = CommandType.Text; 44 return command; 45 } 46 47 /// <summary> 48 /// GetParameter 用於為命令設置參數 49 /// </summary> 50 /// <param name="paramName"></param> 51 /// <param name="paramValue"></param> 52 /// <param name="command"></param> 53 /// <returns></returns> 54 private static DbParameter GetParameter(string paramName, object paramValue, DbCommand command) 55 { 56 DbParameter parameter = command.CreateParameter(); 57 parameter.ParameterName = paramName; 58 parameter.Value = paramValue; 59 return parameter; 60 } 61 62 /// <summary> 63 /// 執行無參的存儲過程 64 /// </summary> 65 /// <param name="sqlCommand">存儲過程名</param> 66 /// <returns></returns> 67 public static int ExecuteNonQueryProc(string sqlCommand) 68 { 69 int result = 0; 70 using (DbConnection connection = GetConnection()) 71 { 72 DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection); 73 connection.Open(); 74 result = command.ExecuteNonQuery(); 75 command.Parameters.Clear(); 76 } 77 return result; 78 } 79 80 /// <summary> 81 /// 執行無返回值有參數的存儲過程 82 /// </summary> 83 /// <param name="sqlCommand">存儲過程名</param> 84 /// <param name="parameters">參數</param> 85 /// <returns></returns> 86 public static int ExecuteNonQueryProc(string sqlCommand, Dictionary<string, object> parameters) 87 { 88 int result = 0; 89 using (DbConnection connection = GetConnection()) 90 { 91 DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection); 92 foreach (KeyValuePair<string, object> p in parameters) 93 { 94 command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 95 } 96 connection.Open(); 97 result = command.ExecuteNonQuery(); 98 command.Parameters.Clear(); 99 } 100 return result; 101 } 102 103 104 /// <summary> 105 /// 執行無返回值的sql語句 106 /// </summary> 107 /// <param name="sqlCommand"></param> 108 /// <param name="parameters"></param> 109 public static int ExecuteNonQuery(string sqlCommand) 110 { 111 int result = 0; 112 using (DbConnection connection = GetConnection()) 113 { 114 DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection); 115 connection.Open(); 116 result = command.ExecuteNonQuery(); 117 command.Parameters.Clear(); 118 return result; 119 } 120 } 121 122 /// <summary> 123 /// 執行有參數的sql語句 124 /// </summary> 125 /// <param name="sqlCommand"></param> 126 /// <param name="para"></param> 127 /// <returns></returns> 128 public static int ExecuteNonQuery(string sqlCommand, Dictionary<string, object> para) 129 { 130 int result = 0; 131 using (DbConnection connection = GetConnection()) 132 { 133 DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection); 134 foreach (KeyValuePair<string, object> p in para) 135 { 136 command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 137 } 138 connection.Open(); 139 result = command.ExecuteNonQuery(); 140 command.Parameters.Clear(); 141 return result; 142 } 143 } 144 145 /// <summary> 146 /// 執行有返回值無參數的存儲過程 147 /// </summary> 148 /// <param name="cmdText"></param> 149 /// <returns></returns> 150 public static object ExecuteScalarProc(string cmdText) 151 { 152 using (DbConnection connection = GetConnection()) 153 { 154 DbCommand command = GetCommand(cmdText, CommandType.StoredProcedure, connection); 155 connection.Open(); 156 object val = command.ExecuteScalar(); 157 command.Parameters.Clear(); 158 return val; 159 } 160 } 161 162 /// <summary> 163 /// 執行有返回值的有參數的存儲過程 164 /// </summary> 165 /// <param name="cmdText">存儲過程名</param> 166 /// <param name="para">參數</param> 167 /// <returns></returns> 168 public static object ExecuteScalarProc(string cmdText, Dictionary<string, object> para) 169 { 170 using (DbConnection connection = GetConnection()) 171 { 172 DbCommand command = GetCommand(cmdText, CommandType.StoredProcedure, connection); 173 foreach (KeyValuePair<string, object> p in para) 174 { 175 command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 176 } 177 connection.Open(); 178 object val = command.ExecuteScalar(); 179 command.Parameters.Clear(); 180 return val; 181 } 182 } 183 184 /// <summary> 185 /// 執行有返回值的sql語句 186 /// </summary> 187 /// <param name="cmdText"></param> 188 /// <returns></returns> 189 public static object ExecuteScalar(string cmdText) 190 { 191 using (DbConnection connection = GetConnection()) 192 { 193 DbCommand command = GetCommand(cmdText, CommandType.Text, connection); 194 connection.Open(); 195 object val = command.ExecuteScalar(); 196 command.Parameters.Clear(); 197 return val; 198 } 199 } 200 201 /// <summary> 202 /// 執行有返回值有參數的sql語句 203 /// </summary> 204 /// <param name="cmdText"></param> 205 /// <param name="para"></param> 206 /// <returns></returns> 207 public static object ExecuteScalar(string cmdText, Dictionary<string, object> para) 208 { 209 using (DbConnection connection = GetConnection()) 210 { 211 DbCommand command = GetCommand(cmdText, CommandType.Text, connection); 212 foreach (KeyValuePair<string, object> p in para) 213 { 214 command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 215 } 216 connection.Open(); 217 object val = command.ExecuteScalar(); 218 command.Parameters.Clear(); 219 return val; 220 } 221 222 } 223 224 /// <summary> 225 /// 執行無參數的存儲過程,返回DbDataReader對象 226 /// </summary> 227 /// <param name="sqlCommand">存儲過程名</param> 228 /// <returns></returns> 229 public static DbDataReader GetReaderProc(string sqlCommand) 230 { 231 232 try 233 { 234 235 DbConnection connection = GetConnection(); 236 237 DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection); 238 239 connection.Open(); 240 241 DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 242 243 return reader; 244 245 } 246 247 catch (Exception ex) 248 { 249 250 Console.Write("" + ex.Message); 251 252 return null; 253 254 } 255 256 } 257 258 /// <summary> 259 /// 執行有參數的存儲過程,返回DbDataReader對象 260 /// </summary> 261 /// <param name="sqlCommand"></param> 262 /// <param name="parameters"></param> 263 /// <returns></returns> 264 public static DbDataReader GetReaderProc(string sqlCommand, Dictionary<string, object> parameters) 265 { 266 267 try 268 { 269 270 DbConnection connection = GetConnection(); 271 272 DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection); 273 274 foreach (KeyValuePair<string, object> p in parameters) 275 { 276 277 command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 278 279 } 280 281 connection.Open(); 282 283 DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 284 285 command.Parameters.Clear(); 286 287 return reader; 288 289 } 290 291 catch 292 { 293 294 return null; 295 296 } 297 298 } 299 #region 300 301 /// <summary> 302 /// 執行無參數的sql語句,返回DbDataReader對象 303 /// </summary> 304 /// <param name="sqlCommand"></param> 305 /// <returns></returns> 306 public static DbDataReader GetReader(string sqlCommand) 307 { 308 309 try 310 { 311 312 DbConnection connection = GetConnection(); 313 314 DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection); 315 316 connection.Open(); 317 318 DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 319 320 command.Parameters.Clear(); 321 322 return reader; 323 324 } 325 326 catch (Exception ex) 327 { 328 329 Console.Write("" + ex.Message); 330 331 return null; 332 333 } 334 335 } 336 337 #endregion 338 /// <summary> 339 /// 執行有參數的sql語句,返回DbDataReader對象 340 /// </summary> 341 /// <param name="sqlCommand"></param> 342 /// <param name="parameters"></param> 343 /// <returns></returns> 344 public static DbDataReader GetReader(string sqlCommand, Dictionary<string, object> parameters) 345 { 346 try 347 { 348 DbConnection connection = GetConnection(); 349 DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection); 350 foreach (KeyValuePair<string, object> p in parameters) 351 { 352 command.Parameters.Add(GetParameter(p.Key, p.Value, command)); 353 } 354 connection.Open(); 355 DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 356 command.Parameters.Clear(); 357 return reader; 358 } 359 catch (Exception ex) 360 { 361 Console.Write("" + ex.Message); 362 return null; 363 } 364 } 365 366 /// <summary> 367 /// 返回DataTable對象 368 /// </summary> 369 /// <param name="safeSql"></param> 370 /// <returns></returns> 371 public static DataTable GetDataSet(string safeSql) 372 { 373 /*DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName); 374 DbConnection connection = GetConnection(); 375 connection.Open(); 376 DbDataAdapter da = _factory.CreateDataAdapter(); 377 da.SelectCommand = connection.CreateCommand(); 378 da.SelectCommand.CommandText = safeSql; 379 DataTable dt = new DataTable(); 380 da.Fill(dt); 381 return dt;*/ 382 383 using (DbConnection connection = GetConnection()) 384 { 385 DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName); 386 DbCommand command = GetCommand(safeSql, CommandType.Text, connection); 387 connection.Open(); 388 DbDataAdapter da = _factory.CreateDataAdapter(); 389 da.SelectCommand = command; 390 DataTable datatable = new DataTable(); 391 da.Fill(datatable); 392 return datatable; 393 } 394 } 395 }
3.創建DAL實現要實現的 介面,主要是對原始數據(資料庫或者文本文件等存放數據的形式)的操作層,而不是指原始數據,也就是說,是對數據的操作,而不是資料庫,具體為業務邏輯層或表示層提供數據服務
引用Model
View Code1 //增 2 public static bool Add(Model.LoginModel user) 3 { 4 string sql = "insert into admin(AdminUID,AdminPWD)values(@AdminUID,@AdminPWD)"; 5 Dictionary<string, object> dic = new Dictionary<string, object>(); 6 dic.Add("@AdminUID",user.AdminUID); 7 dic.Add("@AdminPWD", user.AdminPWD); 8 return DBHelp.ExecuteNonQuery(sql, dic) > 0 ? true : false; 9 } 10 //刪 11 public static bool Delete(int UserID) 12 { 13 string sql = "delete from admin where AdminID=@AdminID"; 14 Dictionary<string, object> dic = new Dictionary<string, object>(); 15 dic.Add("AdminID",UserID); 16 return DBHelp.ExecuteNonQuery(sql, dic) > 0 ? true : false; 17 } 18 //改 19 public static bool Modelfy(Model.LoginModel user) 20 { 21 string sql = "update admin set AdminUID=@AdminUID and AdminPWD=@AdminPWD "; 22 Dictionary<string, object> dic = new Dictionary<string, object>(); 23 dic.Add("AdminUID", user.AdminUID); 24 dic.Add("AdminPWD", user.AdminPWD); 25 return DBHelp.ExecuteNonQuery(