⒈添加 NuGet 包
1 Install-Package Dapper
⒉封装数据库类型
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 6 namespace DapperDemo.Data 7 { 8 ///9 /// 数据库类型 10 /// 11 public enum DbType 12 { 13 Access, 14 SqlServer, 15 Oracle, 16 MySql, 17 SqlLite 18 } 19 }
⒊封装数据库连接仓库
1 using Microsoft.Extensions.Configuration; 2 using MySql.Data.MySqlClient; 3 using System; 4 using System.Collections.Concurrent; 5 using System.Collections.Generic; 6 using System.Data; 7 using System.Data.SqlClient; 8 using System.Linq; 9 using System.Threading.Tasks; 10 11 namespace DapperDemo.Data 12 { 13 public class DbConnectionFactory 14 { 15 ///16 /// 数据库连接字符串缓存 17 /// 18 private static ConcurrentDictionary<string, string> connStrDict = new ConcurrentDictionary<string, string>(); 19 private static IConfiguration Configuration { get; } 20 private static string GetConnString(string dbKey) 21 { 22 string connString = string.Empty; 23 if (connStrDict.Keys.Contains(dbKey)) 24 { 25 connString = connStrDict[dbKey]; 26 } 27 else 28 { 29 connString = Configuration[$"ConnectionStrings:{dbKey}"]; 30 connStrDict.TryAdd(dbKey, connString); 31 } 32 return connString; 33 } 34 35 public static IDbConnection GetConnection(string dbKey, DbType dbType = DbType.SqlServer) 36 { 37 IDbConnection connObj = null; 38 switch (dbType) 39 { 40 case DbType.SqlServer: 41 connObj = new SqlConnection(GetConnString(dbKey)); 42 break; 43 case DbType.MySql: 44 connObj = new MySqlConnection(GetConnString(dbKey)); 45 break; 46 case DbType.Access: 47 //connObj = new OleDbConnection(GetConnString(dbKey)); 48 break; 49 case DbType.SqlLite: 50 break; 51 case DbType.Oracle: 52 break; 53 } 54 55 if (connObj.State != ConnectionState.Open) 56 { 57 connObj.Open(); 58 } 59 60 return connObj; 61 } 62 63 /// 64 /// 获取数据连接 65 /// 66 /// 67 /// 68 /// 69 public static IDbConnection GetConnectionByConnString(string connString, DbType dbType = DbType.SqlServer) 70 { 71 IDbConnection connObj = null; 72 switch (dbType) 73 { 74 case DbType.SqlServer: 75 connObj = new SqlConnection(connString); 76 break; 77 case DbType.MySql: 78 connObj = new MySqlConnection(connString); 79 break; 80 case DbType.Access: 81 //connObj = new OleDbConnection(connString); 82 break; 83 case DbType.SqlLite: 84 break; 85 case DbType.Oracle: 86 break; 87 } 88 89 if (connObj.State != ConnectionState.Open) 90 { 91 connObj.Open(); 92 } 93 94 return connObj; 95 } 96 } 97 }
⒋封装数据库常见方法
1 using Dapper; 2 using System; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Linq; 7 using System.Threading.Tasks; 8 9 namespace DapperDemo.Data 10 { 11 public class DbRepository 12 { 13 private IDbConnection _connection { get; set; } 14 public string dbConnKey { get; set; } 15 public DbType dbType { get; set; } 16 17 public DbRepository(string dbConnKey, DbType dbType = DbType.SqlServer) 18 { 19 this.dbConnKey = dbConnKey; 20 this.dbType = dbType; 21 } 22 23 public DbRepository() 24 { 25 26 } 27 28 public void Init(string dbConnKey, DbType dbType = DbType.SqlServer) 29 { 30 this.dbConnKey = dbConnKey; 31 this.dbType = dbType; 32 } 33 34 #region 属性 35 36 ///37 /// 获取数据库连接 38 /// 39 public IDbConnection Connection 40 { 41 get 42 { 43 if (_connection == null) 44 { 45 _connection = DbConnectionFactory.GetConnection(dbConnKey, dbType); 46 } 47 return _connection; 48 } 49 } 50 51 /// 52 /// 事务对象 53 /// 54 public IDbTransaction dbTransaction { get; set; } 55 56 #endregion 57 58 #region 事务提交 59 60 /// 61 /// 事务开始 62 /// 63 /// 64 public DbRepository BeginTrans() 65 { 66 dbTransaction = Connection.BeginTransaction(); 67 return this; 68 } 69 70 /// 71 /// 提交当前操作的结果 72 /// 73 public int Commit() 74 { 75 try 76 { 77 if (dbTransaction != null) 78 { 79 dbTransaction.Commit(); 80 this.Close(); 81 } 82 return 1; 83 } 84 catch (Exception ex) 85 { 86 if (ex.InnerException != null && ex.InnerException.InnerException is SqlException) 87 { 88 SqlException sqlEx = ex.InnerException.InnerException as SqlException; 89 } 90 throw; 91 } 92 finally 93 { 94 if (dbTransaction == null) 95 { 96 this.Close(); 97 } 98 } 99 } 100 101 /// 102 /// 把当前操作回滚成未提交状态 103 /// 104 public void Rollback() 105 { 106 this.dbTransaction.Rollback(); 107 this.dbTransaction.Dispose(); 108 this.Close(); 109 } 110 111 /// 112 /// 关闭连接 内存回收 113 /// 114 public void Close() 115 { 116 IDbConnection dbConnection = dbTransaction.Connection; 117 if (dbConnection != null && dbConnection.State != ConnectionState.Closed) 118 { 119 dbConnection.Close(); 120 } 121 122 } 123 124 #endregion 125 126 #region 实例方法 127 128 #region 查询 129 130 /// 131 /// 查询 132 /// 133 /// 返回类型 134 /// sql语句 135 /// 数据库连接 136 /// sql查询参数 137 /// 超时时间 138 /// 命令类型 139 /// 140 public T QueryFirst (string sql, object param = null, bool buffered = true, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 141 { 142 if (dbTransaction == null) 143 { 144 using (var dbConn = Connection) 145 { 146 return dbConn.QueryFirstOrDefault (sql, param, null, commandTimeout, commandType); 147 } 148 } 149 else 150 { 151 return dbTransaction.Connection.QueryFirstOrDefault (sql, param, dbTransaction, commandTimeout, commandType); 152 } 153 154 } 155 156 /// 157 /// 查询(异步版本) 158 /// 159 /// 返回类型 160 /// sql语句 161 /// 数据库连接 162 /// sql查询参数 163 /// 超时时间 164 /// 命令类型 165 /// 166 public async Task QueryFirstAsync (string sql, object param = null, bool buffered = true, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 167 { 168 if (dbTransaction == null) 169 { 170 using (var dbConn = Connection) 171 { 172 return await dbConn.QueryFirstOrDefaultAsync (sql, param, null, commandTimeout, commandType); 173 } 174 } 175 else 176 { 177 return await dbTransaction.Connection.QueryFirstOrDefaultAsync (sql, param, dbTransaction, commandTimeout, commandType); 178 } 179 180 } 181 182 183 /// 184 /// 查询 185 /// 186 /// 返回类型 187 /// sql语句 188 /// 数据库连接 189 /// sql查询参数 190 /// 是否缓冲 191 /// 超时时间 192 /// 命令类型 193 /// 194 public IEnumerable Query (string sql, object param = null, bool buffered = true, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 195 { 196 if (dbTransaction == null) 197 { 198 using (var dbConn = Connection) 199 { 200 return dbConn.Query (sql, param, null, buffered, commandTimeout, commandType); 201 } 202 } 203 else 204 { 205 return dbTransaction.Connection.Query (sql, param, dbTransaction, buffered, commandTimeout, commandType); 206 } 207 208 } 209 210 211 /// 212 /// 查询(异步版本) 213 /// 214 /// 返回类型 215 /// sql语句 216 /// 数据库连接 217 /// sql查询参数 218 /// 是否缓冲 219 /// 超时时间 220 /// 命令类型 221 /// 222 public async Task > QueryAsync (string sql, object param = null, bool buffered = true, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 223 { 224 if (dbTransaction == null) 225 { 226 using (var dbConn = Connection) 227 { 228 return await dbConn.QueryAsync (sql, param, null, commandTimeout, commandType); 229 } 230 } 231 else 232 { 233 return await dbTransaction.Connection.QueryAsync (sql, param, dbTransaction, commandTimeout, commandType); 234 } 235 236 } 237 238 239 240 /// 241 /// 查询返回 IDataReader 242 /// 243 /// sql语句 244 /// 数据库连接 245 /// sql查询参数 246 /// 超时时间 247 /// 命令类型 248 /// 249 public IDataReader ExecuteReader(string sql, object param = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 250 { 251 if (dbTransaction == null) 252 { 253 using (var dbConn = Connection) 254 { 255 return dbConn.ExecuteReader(sql, param, null, commandTimeout, commandType); 256 } 257 } 258 else 259 { 260 return dbTransaction.Connection.ExecuteReader(sql, param, dbTransaction, commandTimeout, commandType); 261 } 262 } 263 264 /// 265 /// 查询单个返回值 266 /// 267 /// 返回类型 268 /// sql语句 269 /// 数据库连接 270 /// sql查询参数 271 /// 超时时间 272 /// 命令类型 273 /// 274 public T ExecuteScalar (string sql, object param = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 275 { 276 if (dbTransaction == null) 277 { 278 using (var dbConn = Connection) 279 { 280 return dbConn.ExecuteScalar (sql, param, null, commandTimeout, commandType); 281 } 282 } 283 else 284 { 285 return dbTransaction.Connection.ExecuteScalar (sql, param, dbTransaction, commandTimeout, commandType); 286 } 287 288 } 289 #endregion 290 291 /// 292 /// 执行增删改sql 293 /// 294 /// sql 295 /// 数据库连接 296 /// sql查询参数 297 /// 超时时间 298 /// 命令类型 299 /// 300 public int ExecuteSql(string sql, object param = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 301 { 302 if (dbTransaction == null) 303 { 304 using (var dbConn = Connection) 305 { 306 return dbConn.Execute(sql, param, null, commandTimeout, commandType); 307 } 308 } 309 else 310 { 311 return dbTransaction.Connection.Execute(sql, param, dbTransaction); 312 } 313 } 314 315 /// 316 /// 执行增删改sql(异步版本) 317 /// 318 /// sql 319 /// 数据库连接 320 /// sql查询参数 321 /// 超时时间 322 /// 命令类型 323 /// 324 public async Task<int> ExecuteSqlAsync(string sql, object param = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 325 { 326 if (dbTransaction == null) 327 { 328 using (var dbConn = Connection) 329 { 330 return await dbConn.ExecuteAsync(sql, param, null, commandTimeout, commandType); 331 } 332 } 333 else 334 { 335 await dbTransaction.Connection.ExecuteAsync(sql, param, dbTransaction); 336 return 0; 337 } 338 } 339 340 341 #endregion 342 343 #region 静态方法 344 345 #region 查询 346 /// 347 /// 查询 348 /// 349 /// 返回类型 350 /// sql语句 351 /// 数据库连接 352 /// sql查询参数 353 /// 超时时间 354 /// 命令类型 355 /// 356 public static T QueryFirst (string sql, string dbConnKey, object param = null, DbType dbType = DbType.SqlServer, bool buffered = true, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 357 { 358 using (var dbConn = DbConnectionFactory.GetConnection(dbConnKey, dbType)) 359 { 360 return dbConn.QueryFirstOrDefault (sql, param, null, commandTimeout, commandType); 361 } 362 } 363 364 /// 365 /// 查询(异步版本) 366 /// 367 /// 返回类型 368 /// sql语句 369 /// 数据库连接 370 /// sql查询参数 371 /// 超时时间 372 /// 命令类型 373 /// 374 public static async Task QueryFirstAsync (string sql, string dbConnKey, object param = null, DbType dbType = DbType.SqlServer, bool buffered = true, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 375 { 376 using (var dbConn = DbConnectionFactory.GetConnection(dbConnKey, dbType)) 377 { 378 return await dbConn.QueryFirstOrDefaultAsync (sql, param, null, commandTimeout, commandType); 379 } 380 } 381 382 383 /// 384 /// 查询 385 /// 386 /// 返回类型 387 /// sql语句 388 /// 数据库连接 389 /// sql查询参数 390 /// 是否缓冲 391 /// 超时时间 392 /// 命令类型 393 /// 394 public static IEnumerable Query (string sql, string dbConnKey, object param = null, DbType dbType = DbType.SqlServer, bool buffered = true, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 395 { 396 using (var dbConn = DbConnectionFactory.GetConnection(dbConnKey, dbType)) 397 { 398 return dbConn.Query (sql, param, null, buffered, commandTimeout, commandType); 399 } 400 } 401 402 403 /// 404 /// 查询(异步版本) 405 /// 406 /// 返回类型 407 /// sql语句 408 /// 数据库连接 409 /// sql查询参数 410 /// 是否缓冲 411 /// 超时时间 412 /// 命令类型 413 /// 414 public static async Task > QueryAsync (string sql, string dbConnKey, object param = null, DbType dbType = DbType.SqlServer, bool buffered = true, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 415 { 416 using (var dbConn = DbConnectionFactory.GetConnection(dbConnKey, dbType)) 417 { 418 return await dbConn.QueryAsync (sql, param, null, commandTimeout, commandType); 419 } 420 } 421 422 423 424 /// 425 /// 查询返回 IDataReader 426 /// 427 /// sql语句 428 /// 数据库连接 429 /// sql查询参数 430 /// 超时时间 431 /// 命令类型 432 /// 433 public static IDataReader ExecuteReader(string sql, string dbConnKey, object param = null, DbType dbType = DbType.SqlServer, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 434 { 435 using (var dbConn = DbConnectionFactory.GetConnection(dbConnKey, dbType)) 436 { 437 return dbConn.ExecuteReader(sql, param, null, commandTimeout, commandType); 438 } 439 } 440 441 /// 442 /// 查询单个返回值 443 /// 444 /// 返回类型 445 /// sql语句 446 /// 数据库连接 447 /// sql查询参数 448 /// 超时时间 449 /// 命令类型 450 /// 451 public static T ExecuteScalar (string sql, string dbConnKey, object param = null, DbType dbType = DbType.SqlServer, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 452 { 453 using (var dbConn = DbConnectionFactory.GetConnection(dbConnKey, dbType)) 454 { 455 return dbConn.ExecuteScalar (sql, param, null, commandTimeout, commandType); 456 } 457 } 458 459 #endregion 460 461 #region 增删改 462 463 /// 464 /// 执行增删改sql 465 /// 466 /// sql 467 /// 数据库连接 468 /// sql查询参数 469 /// 超时时间 470 /// 命令类型 471 /// 472 public static int Execute(string sql, string dbkey, object param = null, DbType dbType = DbType.SqlServer, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 473 { 474 using (var dbConn = DbConnectionFactory.GetConnection(dbkey, dbType)) 475 { 476 return dbConn.Execute(sql, param, null, commandTimeout, commandType); 477 } 478 } 479 480 /// 481 /// 执行增删改sql(异步版本) 482 /// 483 /// sql 484 /// 数据库连接 485 /// sql查询参数 486 /// 超时时间 487 /// 命令类型 488 /// 489 public static async Task<int> ExecuteAsync(string sql, string dbkey, object param = null, DbType dbType = DbType.SqlServer, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 490 { 491 using (var dbConn = DbConnectionFactory.GetConnection(dbkey, dbType)) 492 { 493 return await dbConn.ExecuteAsync(sql, param, null, commandTimeout, commandType); 494 } 495 } 496 497 498 /// 499 /// 执行 DynamicQuery.GetInsertQuery* 方法生成的Sql 返回标识值 500 /// 501 /// 502 /// 503 /// 504 /// 505 /// 506 /// 507 /// 508 public static T ExecuteInsertSql (string sql, string dbKey, object param = null, DbType dbType = DbType.SqlServer, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?)) 509 { 510 using (var dbConn = DbConnectionFactory.GetConnection(dbKey, dbType)) 511 { 512 return dbConn.QueryFirstOrDefault (sql, param, null, commandTimeout, commandType); 513 } 514 } 515 516 #endregion 517 518 #endregion 519 520 } 521 }