自用数据库类升级,支持SQLITE,ACCESS,SQLSERVER,支持直接对象操作,支持类似LINQ的查询语句写法,2.0框架!
DBHelper 源码:
DBHelper
1 /// <summary> 2 /// 数据库操作类 3 /// </summary> 4 public sealed class DB : IDisposable 5 { 6 #region 数据库类型枚举 7 /// <summary> 8 /// 数据库类型 9 /// </summary> 10 public enum DBType 11 { 12 /// <summary> 13 /// SQLSERVER 14 /// </summary> 15 SQLSERVER, 16 /// <summary> 17 /// MDB 18 /// </summary> 19 MDB, 20 /// <summary> 21 /// SQLITE 22 /// </summary> 23 SQLITE 24 } 25 #endregion 26 27 #region 公共成员 28 /// <summary> 29 /// 连接字符串(可用本类的静态方法生成) 30 /// GetSQLConnectionString 31 /// GetMdbConnectionString 32 /// GetSQLiteConnectionString 33 /// </summary> 34 public string ConnectionString { get; set; } //连接字符串 35 36 DBType _DbType; 37 38 /// <summary> 39 /// 数据库类型 40 /// </summary> 41 public DBType DbType 42 { 43 get { return this._DbType; } 44 set 45 { 46 this._DbType = value; 47 switch (value) 48 { 49 case DBType.SQLSERVER: 50 Factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); 51 break; 52 case DBType.MDB: 53 Factory = DbProviderFactories.GetFactory("System.Data.OleDb"); 54 break; 55 case DBType.SQLITE: 56 Factory = DbProviderFactories.GetFactory("System.Data.SQLite"); 57 break; 58 } 59 } 60 } //数据库类型 61 62 /// <summary> 63 /// 查询字符串 64 /// </summary> 65 public string CommandText { get; set; } //查询语句 66 67 #endregion 68 69 #region 私有成员 70 71 private DbParameterCollection Parameters { get; set; } //参数集合 72 73 #endregion 74 75 #region 初始成员 76 77 private DbConnection Conn = null; //连接对象 78 79 private DbProviderFactory Factory = null; //数据库工厂 80 81 private List<myTran> TranList = new List<myTran>(); //事务集合 82 83 #endregion 84 85 #region 构造函数 86 87 88 /// <summary> 89 /// 构造函数 90 /// </summary> 91 /// <param name="dbType">数据库类别,可使用DB.DBType获得</param> 92 /// <param name="connectionString"> 93 /// 连接字符串,可使用本类的静态方法获得 94 /// GetSQLConnectionString 95 /// GetMdbConnectionString 96 /// GetSQLiteConnectionString 97 /// </param> 98 public DB(DBType dbType, string connectionString) 99 { 100 this.DbType = dbType; 101 this.ConnectionString = connectionString; 102 this.Parameters = Factory.CreateCommand().Parameters; 103 this.Open(); 104 } 105 #endregion 106 107 #region 初始化与自动释放 108 /// <summary> 109 /// 打开数据库 110 /// </summary> 111 public void Open() 112 { 113 try 114 { 115 if (Conn == null) 116 { 117 Conn = Factory.CreateConnection(); 118 Conn.ConnectionString = this.ConnectionString; 119 Conn.Open(); 120 } 121 else 122 { 123 if (Conn.State == ConnectionState.Closed) 124 Conn.Open(); 125 } 126 } 127 catch (Exception) 128 { 129 throw; 130 } 131 } 132 133 /// <summary> 134 /// 关闭数据库 135 /// </summary> 136 public void Close() 137 { 138 try 139 { 140 if (Conn.State == ConnectionState.Open) 141 Conn.Close(); 142 } 143 catch (Exception) 144 { 145 throw; 146 } 147 } 148 149 /// <summary> 150 /// 自动释放资源 151 /// </summary> 152 public void Dispose() 153 { 154 try 155 { 156 this.CommandText = string.Empty; 157 this.Parameters = null; 158 this.ConnectionString = string.Empty; 159 this.TranList = null; 160 Close(); 161 } 162 catch (Exception) 163 { 164 throw; 165 } 166 } 167 #endregion 168 169 #region 添加查询参数 170 /// <summary> 171 /// 添加查询参数 172 /// </summary> 173 /// <param name="name">参数名 如:@Name</param> 174 /// <param name="value">参数值 如:"Name"</param> 175 public void AddParameter(string name, object value) 176 { 177 var pa = Factory.CreateParameter(); 178 pa.ParameterName = name; 179 pa.Value = value; 180 this.Parameters.Add(pa); 181 } 182 183 /// <summary> 184 /// 添加对象为查询参数 185 /// </summary> 186 /// <typeparam name="T">对象类型</typeparam> 187 /// <param name="model">对象</param> 188 public void AddParameters<T>(T model) where T : class,new() 189 { 190 Type t = typeof(T); 191 Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => 192 { 193 AddParameter("@" + p.Name, p.GetValue(model, null)); 194 }); 195 } 196 197 /// <summary> 198 /// 顺序添加数组为查询参数 199 /// </summary> 200 /// <param name="names">参数名 如:{"@Name","@Age"}</param> 201 /// <param name="values">参数值 如:{"Name",20}</param> 202 public void AddParameters(string[] names, object[] values) 203 { 204 if (names.Length != values.Length) 205 throw new Exception("参数名称跟参数值数量不匹配!"); 206 for (var i = 0; i < names.Length; i++) 207 { 208 var pa = Factory.CreateParameter(); 209 pa.ParameterName = names[i]; 210 pa.Value = values[i]; 211 this.Parameters.Add(pa); 212 } 213 } 214 #endregion 215 216 #region 创建查询参数 217 /// <summary> 218 /// 创建查询参数 219 /// </summary> 220 /// <param name="name">参数名 如:"@Name"</param> 221 /// <param name="value">参数值 如:"Name"</param> 222 /// <returns></returns> 223 public DbParameter CreateParameter(string name, object value) 224 { 225 var pa = Factory.CreateParameter(); 226 pa.ParameterName = name; 227 pa.Value = value; 228 return pa; 229 } 230 231 /// <summary> 232 /// 通过数组创建查询参数 233 /// </summary> 234 /// <param name="names">参数名 如:{"@Name","@Age"}</param> 235 /// <param name="values">参数值 如:{"Name",20}</param> 236 /// <returns></returns> 237 public List<DbParameter> CreateParameters(string[] names, object[] values) 238 { 239 if (names.Length != values.Length) 240 throw new Exception("参数名称跟参数值数量不匹配!"); 241 var parameters = new List<DbParameter>(); 242 for (var i = 0; i < names.Length; i++) 243 { 244 parameters.Add(CreateParameter(names[i], values[i])); 245 } 246 return parameters; 247 } 248 249 250 /// <summary> 251 /// 通过对象创建查询参数 252 /// </summary> 253 /// <typeparam name="T">对象类型</typeparam> 254 /// <param name="model">对象</param> 255 /// <returns></returns> 256 public List<DbParameter> CreateParameters<T>(T model) where T : class,new() 257 { 258 var parameters = new List<DbParameter>(); 259 Type t = typeof(T); 260 Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => 261 { 262 parameters.Add(CreateParameter(p.Name, p.GetValue(model, null))); 263 }); 264 return parameters; 265 } 266 #endregion 267 268 #region 清除查询字符串和查询参数 269 /// <summary> 270 /// 清除查询字符串和查询参数 271 /// </summary> 272 void Clear() 273 { 274 this.CommandText = ""; 275 if (this.Parameters != null) 276 this.Parameters.Clear(); 277 } 278 #endregion 279 280 #region 返回一个DataTable 281 /// <summary> 282 /// 返回一个DataTable 283 /// </summary> 284 public DataTable ExecuteDataTable() 285 { 286 try 287 { 288 using (DbCommand cmd = Factory.CreateCommand()) 289 { 290 Open(); 291 cmd.Connection = this.Conn; 292 cmd.CommandText = this.CommandText; 293 //cmd.Parameters.AddRange(this.Parameters); 294 if (this.Parameters != null) 295 foreach (var para in this.Parameters) 296 { 297 var p = cmd.CreateParameter(); 298 p.ParameterName = (para as DbParameter).ParameterName; 299 p.Value = (para as DbParameter).Value; 300 cmd.Parameters.Add(p); 301 } 302 Clear(); 303 304 DbDataReader dr = cmd.ExecuteReader(); 305 DataTable dt = new DataTable(); 306 dt.Load(dr); 307 return dt; 308 } 309 } 310 catch (Exception) 311 { 312 throw; 313 } 314 finally 315 { 316 Clear(); 317 } 318 } 319 #endregion 320 321 #region 执行一条更新语句 322 /// <summary> 323 /// 执行一条更新语句 324 /// </summary> 325 public int ExecuteNonQuery() 326 { 327 try 328 { 329 using (DbCommand cmd = Factory.CreateCommand()) 330 { 331 Open(); 332 cmd.Connection = this.Conn; 333 cmd.CommandText = this.CommandText; 334 if (this.Parameters != null) 335 foreach (var para in this.Parameters) 336 { 337 var p = cmd.CreateParameter(); 338 p.ParameterName = (para as DbParameter).ParameterName; 339 p.Value = (para as DbParameter).Value; 340 cmd.Parameters.Add(p); 341 } 342 Clear(); 343 if (this.Conn.State == ConnectionState.Closed) 344 Open(); 345 return cmd.ExecuteNonQuery(); 346 } 347 } 348 catch (Exception) 349 { 350 throw; 351 } 352 finally 353 { 354 Clear(); 355 } 356 } 357 #endregion 358 359 #region 返回首行首列 360 /// <summary> 361 /// 返回首行首列 362 /// </summary> 363 /// <returns></returns> 364 public object ExecuteScalar() 365 { 366 try 367 { 368 using (var cmd = Factory.CreateCommand()) 369 { 370 Open(); 371 cmd.Connection = this.Conn; 372 cmd.CommandText = this.CommandText; 373 if (this.Parameters != null) 374 foreach (var para in this.Parameters) 375 { 376 var p = cmd.CreateParameter(); 377 p.ParameterName = (para as DbParameter).ParameterName; 378 p.Value = (para as DbParameter).Value; 379 cmd.Parameters.Add(p); 380 } 381 Clear(); 382 if (this.Conn.State == ConnectionState.Closed) 383 Open(); 384 return cmd.ExecuteScalar(); 385 } 386 } 387 catch (Exception) 388 { 389 throw; 390 } 391 finally 392 { 393 Clear(); 394 } 395 } 396 397 /// <summary> 398 /// 返回首行首列 399 /// </summary> 400 /// <returns></returns> 401 public int ExecuteScalarToInt() 402 { 403 try 404 { 405 using (var cmd = Factory.CreateCommand()) 406 { 407 Open(); 408 cmd.Connection = this.Conn; 409 cmd.CommandText = this.CommandText; 410 if (this.Parameters != null) 411 foreach (var para in this.Parameters) 412 { 413 var p = cmd.CreateParameter(); 414 p.ParameterName = (para as DbParameter).ParameterName; 415 p.Value = (para as DbParameter).Value; 416 cmd.Parameters.Add(p); 417 } 418 Clear(); 419 if (this.Conn.State == ConnectionState.Closed) 420 Open(); 421 return int.Parse(cmd.ExecuteScalar().ToString()); 422 } 423 } 424 catch (Exception) 425 { 426 throw; 427 } 428 finally 429 { 430 Clear(); 431 } 432 } 433 #endregion 434 435 #region 自定义事务类 436 class myTran 437 { 438 public string queryString { get; set; } 439 public List<DbParameter> parameters { get; set; } 440 441 public myTran(string queryString, List<DbParameter> parameters) 442 { 443 this.queryString = queryString; 444 this.parameters = parameters; 445 } 446 } 447 #endregion 448 449 #region 添加事务 450 /// <summary> 451 /// 添加事务 452 /// </summary> 453 /// <param name="queryString">查询语句</param> 454 /// <param name="parameters">参数列表 可通过本类的CreateParameters方法生成</param> 455 public void AddTran(string queryString, List<DbParameter> parameters) 456 { 457 var tran = new myTran(queryString, parameters); 458 TranList.Add(tran); 459 } 460 461 /// <summary> 462 /// 添加事务 463 /// </summary> 464 /// <param name="queryString">查询语句</param> 465 /// <param name="parameter">参数 可通过本类的CreateParameter方法生成</param> 466 public void AddTran(string queryString, DbParameter parameter) 467 { 468 List<DbParameter> paras = new List<DbParameter>(); 469 if (parameter != null) 470 paras.Add(parameter); 471 var tran = new myTran(queryString, paras); 472 TranList.Add(tran); 473 } 474 #endregion 475 476 #region 清除事务 477 void ClearTran() 478 { 479 TranList.Clear(); 480 } 481 #endregion 482 483 #region 执行事务 484 /// <summary> 485 /// 执行事务 486 /// </summary> 487 public void ExecuteTran() 488 { 489 try 490 { 491 using (DbTransaction tran = Conn.BeginTransaction()) 492 { 493 try 494 { 495 if (this.Conn.State == ConnectionState.Closed) 496 Open(); 497 TranList.ForEach(m => 498 { 499 using (var cmd = this.Factory.CreateCommand()) 500 { 501 cmd.Connection = this.Conn; 502 cmd.CommandText = m.queryString; 503 cmd.Transaction = tran; 504 m.parameters.ForEach(n => 505 { 506 cmd.Parameters.Add(n); 507 }); 508 cmd.ExecuteNonQuery(); 509 } 510 }); 511 tran.Commit(); 512 } 513 catch (Exception) 514 { 515 tran.Rollback(); 516 throw; 517 } 518 finally 519 { 520 ClearTran(); 521 } 522 } 523 } 524 catch (Exception) 525 { 526 throw; 527 } 528 finally 529 { 530 ClearTran(); 531 } 532 } 533 #endregion 534 535 #region 根据对象生成更新语句 536 /// <summary> 537 /// 获取更新语句 538 /// </summary> 539 /// <typeparam name="TResult"></typeparam> 540 /// <param name="TableName">表名</param> 541 /// <param name="IndexFieldName">主键ID</param> 542 /// <returns></returns> 543 public string GetUpdateString<TResult>(string TableName, string IndexFieldName) where TResult : class,new() 544 { 545 string rt = "update " + TableName + " set"; 546 Type t = typeof(TResult); 547 Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => 548 { 549 if (p.Name != IndexFieldName) rt += " " + p.Name + " = @" + p.Name + " ,"; 550 }); 551 rt = rt.Substring(0, rt.Length - 2); 552 if (IndexFieldName != null) 553 rt += " where " + IndexFieldName + " = @" + IndexFieldName; 554 return rt; 555 } 556 #endregion 557 558 #region 根据对象生成插入语句 559 /// <summary> 560 /// 获取插入语句 561 /// </summary> 562 /// <typeparam name="TResult"></typeparam> 563 /// <param name="TableName">表名</param> 564 /// <param name="IndexFieldName">主键ID</param> 565 /// <returns></returns> 566 public string GetInsertString<TResult>(string TableName, string IndexFieldName) where TResult : class,new() 567 { 568 string rt = "insert into " + TableName + " ("; 569 Type t = typeof(TResult); 570 Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => 571 { 572 if (p.Name != IndexFieldName) rt += p.Name + " , "; 573 }); 574 rt = rt.Substring(0, rt.Length - 3); 575 rt += ") values ("; 576 Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => 577 { 578 if (p.Name != IndexFieldName) 579 rt += "@" + p.Name + " , "; 580 }); 581 rt = rt.Substring(0, rt.Length - 3); 582 rt += ")"; 583 return rt; 584 } 585 #endregion 586 587 #region 对象操作 588 /// <summary> 589 /// 将对象插入到数据库 590 /// </summary> 591 /// <typeparam name="T">对象类型</typeparam> 592 /// <param name="model">对象</param> 593 /// <param name="TableName">表名</param> 594 /// <param name="IndexFieldName">主键ID</param> 595 /// <returns></returns> 596 public bool InsertModel<T>(T model, string TableName, string IndexFieldName) where T : class,new() 597 { 598 this.CommandText = GetInsertString<T>(TableName, IndexFieldName); 599 this.AddParameters<T>(model); 600 return this.ExecuteNonQuery() > 0; 601 } 602 603 /// <summary> 604 /// 将对象更新到数据库 605 /// </summary> 606 /// <typeparam name="T">对象类型</typeparam> 607 /// <param name="model">对象</param> 608 /// <param name="TableName">表名</param> 609 /// <param name="IndexFieldName">主键ID</param> 610 /// <returns></returns> 611 public bool UpdateModel<T>(T model, string TableName, string IndexFieldName) where T : class,new() 612 { 613 this.CommandText = GetUpdateString<T>(TableName, IndexFieldName); 614 this.AddParameters<T>(model); 615 return this.ExecuteNonQuery() > 0; 616 } 617 #endregion 618 619 #region 数据库静态方法 620 621 #region 生成查询字符串 622 /// <summary> 623 /// 返回SQLSERVER连接字符串 624 /// </summary> 625 /// <param name="serverIp">服务器IP</param> 626 /// <param name="uid">用户名</param> 627 /// <param name="pwd">密码</param> 628 /// <param name="catalog">库名</param> 629 /// <param name="timeout">超时时间</param> 630 /// <returns></returns> 631 public static string GetSQLConnectionString(string serverIp, string uid, string pwd, string catalog, int timeout) 632 { 633 return string.Format("Server={0};User ID={1};PWD={2};Initial Catalog={3};Connect TimeOut={4};", serverIp, uid, pwd, catalog, timeout.ToString()); 634 } 635 636 /// <summary> 637 /// 返回Mdb连接字符串 638 /// </summary> 639 /// <param name="filePath">数据库路径</param> 640 /// <param name="password">数据库密码</param> 641 /// <returns></returns> 642 public static string GetMdbConnectionString(string filePath, string password) 643 { 644 return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Jet OLEDB:Database Password={1}", filePath, password); 645 } 646 647 /// <summary> 648 /// 返回SQLite连接字符串 649 /// </summary> 650 /// <param name="filePath">数据库路径</param> 651 /// <returns></returns> 652 public static string GetSQLiteConnectionString(string filePath) 653 { 654 return string.Format("Data Source={0}", filePath); 655 } 656 #endregion 657 658 #endregion 659 660 #region 好玩的 661 662 int valueCount = 0; 663 664 public DB Select(string fieldName) 665 { 666 this.CommandText = "select " + fieldName; 667 return this; 668 } 669 670 public DB Update(string tableName) 671 { 672 this.CommandText = "update " + (this.DbType == DBType.SQLSERVER ? "" : "from ") + tableName; 673 return this; 674 } 675 676 public DB Insert(string tableName, string[] fieldNames, object[] values) 677 { 678 if (fieldNames.Length != values.Length) 679 throw new ArgumentException("参数fieldNames与values的长度必须一致!"); 680 this.CommandText = "insert into " + tableName + " ("; 681 foreach (var fieldName in fieldNames) 682 { 683 this.CommandText += fieldName + " , "; 684 } 685 this.CommandText = this.CommandText.Substring(0, this.CommandText.Length - 3); 686 this.CommandText += ") values ("; 687 foreach (var fieldName in fieldNames) 688 { 689 this.CommandText += "@" + fieldName + " , "; 690 } 691 this.CommandText = this.CommandText.Substring(0, this.CommandText.Length - 3); 692 this.CommandText += ")"; 693 this.AddParameters(fieldNames, values); 694 return this; 695 } 696 697 public DB Delete(string tableName) 698 { 699 this.CommandText = "delete " + tableName; 700 return this; 701 } 702 703 public DB Set(string fieldName, object value) 704 { 705 AppendSet(fieldName); 706 this.AddParameter("@" + fieldName + valueCount.ToString(), value); 707 return this; 708 } 709 710 public DB Top(int count) 711 { 712 this.CommandText = this.CommandText.Insert(7, " top " + count.ToString()); 713 return this; 714 } 715 716 public DB From(string tableName) 717 { 718 this.CommandText += " from " + tableName; 719 return this; 720 } 721 722 public DB WhereIs(string fieldName, object value) 723 { 724 AppendWhere(); 725 this.CommandText += " " + fieldName + " = @" + fieldName + valueCount.ToString(); 726 this.AddParameter("@" + fieldName + valueCount.ToString(), value); 727 return this; 728 } 729 730 public DB WhereNotIs(string fieldName, object value) 731 { 732 AppendWhere(); 733 this.CommandText += " " + fieldName + (this.DbType == DBType.MDB ? "<> @" : " != @") + fieldName + valueCount.ToString(); 734 this.AddParameter("@" + fieldName + valueCount.ToString(), value); 735 return this; 736 } 737 738 public DB WhereBig(string fieldName, object value) 739 { 740 AppendWhere(); 741 this.CommandText += " " + fieldName + " > @" + fieldName + valueCount.ToString(); 742 this.AddParameter("@" + fieldName + valueCount.ToString(), value); 743 return this; 744 } 745 746 public DB WhereSmall(string fieldName, object value) 747 { 748 AppendWhere(); 749 this.CommandText += " " + fieldName + " < @" + fieldName + valueCount.ToString(); 750 this.AddParameter("@" + fieldName + valueCount.ToString(), value); 751 return this; 752 } 753 754 public DB WhereLike(string fieldName, object value) 755 { 756 AppendWhere(); 757 this.CommandText += " " + fieldName + " like @" + fieldName + valueCount.ToString(); 758 this.AddParameter("@" + fieldName + valueCount.ToString(), value); 759 return this; 760 } 761 762 public DB WhereBetween(string fieldName, object value1, object value2) 763 { 764 AppendWhere(); 765 this.CommandText += " " + fieldName + " between @" + fieldName + valueCount.ToString(); 766 this.AddParameter("@" + fieldName + valueCount.ToString(), value1); 767 AppendWhere(); 768 this.CommandText += " @" + fieldName + valueCount.ToString(); 769 this.AddParameter("@" + fieldName + valueCount.ToString(), value2); 770 return this; 771 } 772 773 public enum OrderType 774 { 775 DESC, 776 ASC 777 } 778 public DB OrderBy(string fieldName, OrderType orderby) 779 { 780 this.CommandText += " order by " + fieldName + " " + orderby.ToString(); 781 return this; 782 } 783 784 /// <summary> 785 /// 分页 786 /// </summary> 787 /// <param name="page">从1开始</param> 788 /// <param name="pageSize">页大小</param> 789 /// <returns></returns> 790 public DB Limit(int page, int pageSize) 791 { 792 switch (this.DbType) 793 { 794 case DBType.MDB: 795 796 break; 797 case DBType.SQLITE: 798 this.CommandText += " limit @page , @pageSize"; 799 this.AddParameter("@page", page - 1); 800 this.AddParameter("@pageSize", (page - 1) * pageSize); 801 break; 802 case DBType.SQLSERVER: 803 804 break; 805 } 806 return this; 807 } 808 809 void AppendWhere() 810 { 811 valueCount++; 812 if (this.CommandText.Contains(" where ")) 813 { 814 this.CommandText += " and"; 815 } 816 else 817 this.CommandText += " where"; 818 } 819 820 void AppendSet(string fieldName) 821 { 822 valueCount++; 823 if (this.CommandText.Contains(" set ")) 824 this.CommandText += " , " + fieldName + " = @" + fieldName + valueCount.ToString(); 825 else 826 this.CommandText += " set " + fieldName + " = @" + fieldName + valueCount.ToString(); 827 } 828 #endregion 829 }
使用案例7个:
使用案例
1 class Test 2 { 3 #region 静态方法,获取数据库操作对象 4 private readonly DB GetDB() 5 { 6 return new DB(DB.DBType.SQLSERVER, DB.GetSQLConnectionString(".", "sa", "sa", "Post", 30)); 7 } 8 #endregion 9 10 #region 实体类 11 class Model 12 { 13 public int Id { get; set; } 14 public string PostNum { get; set; } 15 } 16 #endregion 17 18 #region 获取条目 19 public int GetCount() 20 { 21 using (var db = GetDB()) 22 { 23 return db 24 .Select("count(*)") 25 .From("PostTable") 26 .ExecuteScalarToInt(); 27 } 28 } 29 #endregion 30 31 #region 获取实体列表 32 public List<Model> GetList(int count) 33 { 34 using (var db = GetDB()) 35 { 36 var dt = db.Select("Id,PostNum").From("PostTable").Top(count).ExecuteDataTable(); 37 var list = new List<Model>(); 38 foreach (System.Data.DataRow row in dt.Rows) 39 { 40 Model model = new Model 41 { 42 Id = int.Parse(row["Id"].ToString()), 43 PostNum = row["PostNum"].ToString() 44 }; 45 list.Add(model); 46 } 47 return list; 48 } 49 } 50 #endregion 51 52 #region 插入实体方法1 53 public bool Insert(Model model) 54 { 55 using (var db = GetDB()) 56 { 57 return db.InsertModel(model, "PostTable", "Id"); 58 } 59 } 60 #endregion 61 62 #region 插入实体方法2 63 public bool Insert(int id, string postNum) 64 { 65 using (var db = GetDB()) 66 { 67 return db.Insert("PostTable", 68 new string[] { "Id", "PostNum" }, 69 new object[] { id, postNum }) 70 .ExecuteNonQuery() > 0; 71 } 72 } 73 #endregion 74 75 #region 删除 76 public bool Delete(int id) 77 { 78 using (var db = GetDB()) 79 { 80 return db.Delete("PostTable").WhereIs("Id", id).ExecuteNonQuery() > 0; 81 } 82 } 83 #endregion 84 85 #region 更新实体方法1 86 public bool Update(Model model) 87 { 88 using (var db = GetDB()) 89 { 90 return db.UpdateModel(model, "PostTable", "Id"); 91 } 92 } 93 #endregion 94 95 #region 更新实体方法2 96 public bool Update(int id, string postNum) 97 { 98 using (var db = GetDB()) 99 { 100 return db 101 .Update("PostTable") 102 .Set("PostNum", postNum) 103 .WhereIs("Id", id) 104 .ExecuteNonQuery() > 0; 105 } 106 } 107 #endregion 108 109 #region 查询实体 110 public Model GetModel(int id) 111 { 112 using (var db = GetDB()) 113 { 114 var dt = db.Select("Id,PostNum").From("PostTable").WhereIs("Id", id).ExecuteDataTable(); 115 if (dt.Rows.Count > 0) 116 { 117 Model model = new Model 118 { 119 Id = int.Parse(dt.Rows[0].ToString()), 120 PostNum = dt.Rows[0].ToString() 121 }; 122 return model; 123 } 124 else 125 return null; 126 } 127 } 128 #endregion 129 130 }
简单举了几个例子,其他的大家可以自己在使用中体会!(献丑了,高手飘过哦~~呵呵!)