EnterpriseLibrary6+SQLite3
1.web.config配置:
1 <configuration> 2 <configSections> 3 <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> 4 </configSections> 5 <dataConfiguration defaultDatabase="PeigaoDB" > 6 <providerMappings> 7 <add databaseType="Kysd.Peigao.Data.SQLiteDatabase, Kysd.Peigao.Data" name="System.Data.SQLite.EL6" /> 8 </providerMappings> 9 </dataConfiguration> 10 <connectionStrings> 11 <add name="PeigaoDB" providerName="System.Data.SQLite.EL6" connectionString="Data Source=|DataDirectory|\PeigaoDB.db;Version=3;Password=密码" /> 12 </connectionStrings> 13 <system.data> 14 <DbProviderFactories> 15 <remove invariant="System.Data.SQLite" /> 16 <add name="SQLite Data Provider EL6" invariant="System.Data.SQLite.EL6" description="EnterPrise Library 6 Data Provider for SQLite" type="Kysd.Peigao.Data.SQLiteDatabase, Kysd.Peigao.Data" /> 17 <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /> 18 </DbProviderFactories> 19 </system.data> 20 </configuration>
2.Provider代码实现
参考:http://entlibcontrib.codeplex.com/SourceControl/latest#EnterpriseLibraryContrib/src/Data/SqlLite/SQLiteDatabase.cs
1 using System; 2 using System.Data.Common; 3 using System.Data.SQLite; 4 using Microsoft.Practices.EnterpriseLibrary.Data; 5 using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; 6 using System.Text.RegularExpressions; 7 8 namespace Kysd.Peigao.Data 9 { 10 /// <summary> 11 /// Sqlite Connection Provider 12 /// </summary> 13 /// <author>Manuel Soler (ModMa) 2013</author> 14 /// <remarks>Based in a old source of EntLibContrib. This Sqlite 3.x has specials functions like Enviorement variables support 15 /// <br />This version is not official!</remarks> 16 [ConfigurationElementType(typeof(SQLiteDatabaseData))] 17 public class SQLiteDatabase : Database 18 { 19 /// <summary> 20 /// Initializes a new instance of the SQLiteDatabase class. 21 /// </summary> 22 public SQLiteDatabase(string connectionString) : base(connectionString, SQLiteFactory.Instance) 23 { 24 } 25 26 /// <summary> 27 /// Creates a connection for this database. 28 /// </summary> 29 /// <returns> 30 /// The <see cref="DbConnection"/> for this database. 31 /// </returns> 32 /// <seealso cref="DbConnection"/> 33 /// <remarks>This system use custom variables</remarks> 34 public override System.Data.Common.DbConnection CreateConnection() 35 { 36 //return new System.Data.SQLite.SQLiteConnection(connectionString); 37 var newConnection = this.DbProviderFactory.CreateConnection(); 38 newConnection.ConnectionString = Regex.Replace(this.ConnectionString, @"\|CurrentDate\|", 39 DateTime.Now.ToString("yyyy-MM-dd"), RegexOptions.IgnoreCase); //like |DataDirectory| 40 41 return newConnection; 42 } 43 44 ///// <summary> 45 ///// Gets the SQL string command. 46 ///// </summary> 47 ///// <param name="p">The p.</param> 48 ///// <returns></returns> 49 //public System.Data.Common.DbCommand GetSqlStringCommand(string p) 50 //{ 51 // return new System.Data.SQLite.SQLiteCommand(p); 52 //} 53 54 /// <summary> 55 /// Retrieves parameter information from the stored procedure specified in the <see cref="SQLiteCommand"/> and populates the Parameters collection of the specified <see cref="SQLiteCommand"/> object. 56 /// </summary> 57 /// <param name="discoveryCommand">The <see cref="SQLiteCommand"/> to do the discovery.</param> 58 protected override void DeriveParameters(System.Data.Common.DbCommand discoveryCommand) 59 { 60 throw new Exception("The method or operation is not implemented."); 61 } 62 } 63 }
1 using Microsoft.Practices.EnterpriseLibrary.Data; 2 using Microsoft.Practices.EnterpriseLibrary.Data.Configuration; 3 using System; 4 using System.Configuration; 5 6 namespace Kysd.Peigao.Data 7 { 8 9 /// <summary> 10 /// Represents the process to build an instance of <see cref="SQLiteDatabase"/> described by configuration information. 11 /// </summary> 12 public class SQLiteDatabaseData : DatabaseData 13 { 14 public SQLiteDatabaseData(ConnectionStringSettings connectionStringSettings, Func<string, ConfigurationSection> configurationSource) 15 : base(connectionStringSettings, configurationSource) 16 { 17 } 18 19 public override Database BuildDatabase() 20 { 21 return new SQLiteDatabase(this.ConnectionStringSettings.ConnectionString); 22 } 23 } 24 25 }
1 using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; 2 using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design; 3 using Microsoft.Practices.EnterpriseLibrary.Data; 4 using Microsoft.Practices.EnterpriseLibrary.Data.Configuration; 5 using System; 6 using System.Collections.Generic; 7 using System.Data; 8 using System.Data.SQLite; 9 10 namespace Kysd.Peigao.Data 11 { 12 [AddSateliteProviderCommand("connectionStrings", typeof(DatabaseSettings), "DefaultDatabase", "DatabaseInstanceName")] 13 public class DataHelper 14 { 15 private string dbName; 16 private Database dbo = null; 17 18 /// <summary> 19 /// 构造函数 20 /// </summary> 21 /// <param name="dbName">数据库名称</param> 22 public DataHelper(string dbName) 23 { 24 this.dbName = dbName; 25 this.dbo = InitDatabase(dbName); 26 } 27 28 #region ExecuteNonQuery 29 30 public int ExecuteNonQuery(string cmdText) 31 { 32 return ExecuteNonQuery(CommandType.Text, cmdText, null); 33 } 34 35 public int ExecuteNonQuery(CommandType cmdType, string cmdText) 36 { 37 return ExecuteNonQuery(cmdType, cmdText, null); 38 } 39 40 public int ExecuteNonQuery(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 41 { 42 try 43 { 44 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 45 BeforeExecute(cmdText); 46 int val = dbo.ExecuteNonQuery(SQLiteCommand); 47 SQLiteCommand.Parameters.Clear(); 48 OnSuccess(cmdText); 49 return val; 50 } 51 catch (Exception ex) 52 { 53 OnError(cmdText, ex); 54 throw ex; 55 } 56 } 57 58 public int ExecuteNonQuery(CommandType cmdType, string cmdText, int commandTimeout, params SQLiteParameter[] commandParameters) 59 { 60 try 61 { 62 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 63 SQLiteCommand.CommandTimeout = commandTimeout; 64 BeforeExecute(cmdText); 65 int val = dbo.ExecuteNonQuery(SQLiteCommand); 66 SQLiteCommand.Parameters.Clear(); 67 OnSuccess(cmdText); 68 return val; 69 } 70 catch (Exception ex) 71 { 72 OnError(cmdText, ex); 73 throw ex; 74 } 75 } 76 77 public int ExecuteNonQuery(SQLiteTransaction trans, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 78 { 79 try 80 { 81 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 82 BeforeExecute(cmdText); 83 int val = dbo.ExecuteNonQuery(SQLiteCommand, trans); 84 SQLiteCommand.Parameters.Clear(); 85 OnSuccess(cmdText); 86 return val; 87 } 88 catch (Exception ex) 89 { 90 OnError(cmdText, ex); 91 throw ex; 92 } 93 } 94 95 public int ExecuteNonQuery(SQLiteTransaction trans, CommandType cmdType, string cmdText, int commandTimeout, params SQLiteParameter[] commandParameters) 96 { 97 try 98 { 99 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 100 SQLiteCommand.CommandTimeout = commandTimeout; 101 BeforeExecute(cmdText); 102 int val = dbo.ExecuteNonQuery(SQLiteCommand, trans); 103 SQLiteCommand.Parameters.Clear(); 104 OnSuccess(cmdText); 105 return val; 106 } 107 catch (Exception ex) 108 { 109 OnError(cmdText, ex); 110 throw ex; 111 } 112 } 113 114 #endregion 115 116 #region ExecuteTReader 117 118 public T ExecuteTReader<T>(string cmdText, BindTDataHandler<T> bindDataHandler) where T : class 119 { 120 return ExecuteTReader<T>(CommandType.Text, cmdText, bindDataHandler, null); 121 } 122 123 public T ExecuteTReader<T>(CommandType cmdType, string cmdText, BindTDataHandler<T> bindDataHandler) where T : class 124 { 125 return ExecuteTReader<T>(cmdType, cmdText, bindDataHandler, null); 126 } 127 128 public T ExecuteTReader<T>(CommandType cmdType, string cmdText, BindTDataHandler<T> bindDataHandler, params SQLiteParameter[] commandParameters) where T : class 129 { 130 IDataReader dateReader = null; 131 T list = null; 132 133 try 134 { 135 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 136 BeforeExecute(cmdText); 137 138 using (dateReader = dbo.ExecuteReader(SQLiteCommand)) 139 { 140 OnSuccess(cmdText); 141 142 list = bindDataHandler(dateReader); 143 144 //ExecuteReader 的链接没有关闭前无法得到需要返回的参数值,所以可以在bindDataHandler事件内关闭dateReader 145 if (!dateReader.IsClosed) 146 { 147 dateReader.Close(); 148 } 149 150 SQLiteCommand.Parameters.Clear(); 151 } 152 return list; 153 } 154 catch (Exception ex) 155 { 156 OnError(cmdText, ex); 157 throw ex; 158 } 159 finally 160 { 161 if (dateReader != null && !dateReader.IsClosed) 162 { 163 dateReader.Close(); 164 } 165 } 166 } 167 168 public T ExecuteTReader<T>(CommandType cmdType, string cmdText, int commandTimeout, BindTDataHandler<T> bindDataHandler, params SQLiteParameter[] commandParameters) where T : class 169 { 170 IDataReader dateReader = null; 171 T list = null; 172 173 try 174 { 175 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 176 SQLiteCommand.CommandTimeout = commandTimeout; 177 BeforeExecute(cmdText); 178 179 using (dateReader = dbo.ExecuteReader(SQLiteCommand)) 180 { 181 OnSuccess(cmdText); 182 183 list = bindDataHandler(dateReader); 184 185 //ExecuteReader 的链接没有关闭前无法得到需要返回的参数值,所以可以在bindDataHandler事件内关闭dateReader 186 if (!dateReader.IsClosed) 187 { 188 dateReader.Close(); 189 } 190 191 SQLiteCommand.Parameters.Clear(); 192 } 193 return list; 194 } 195 catch (Exception ex) 196 { 197 OnError(cmdText, ex); 198 throw ex; 199 } 200 finally 201 { 202 if (dateReader != null && !dateReader.IsClosed) 203 { 204 dateReader.Close(); 205 } 206 } 207 } 208 209 public T ExecuteTReader<T>(SQLiteTransaction trans, CommandType cmdType, string cmdText, BindTDataHandler<T> bindDataHandler, params SQLiteParameter[] commandParameters) where T : class 210 { 211 IDataReader dateReader = null; 212 T list = null; 213 214 try 215 { 216 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 217 BeforeExecute(cmdText); 218 219 using (dateReader = dbo.ExecuteReader(SQLiteCommand, trans)) 220 { 221 OnSuccess(cmdText); 222 223 list = bindDataHandler(dateReader); 224 225 //ExecuteReader 的链接没有关闭前无法得到需要返回的参数值,所以可以在bindDataHandler事件内关闭dateReader 226 if (!dateReader.IsClosed) 227 { 228 dateReader.Close(); 229 } 230 231 SQLiteCommand.Parameters.Clear(); 232 } 233 return list; 234 } 235 catch (Exception ex) 236 { 237 OnError(cmdText, ex); 238 throw ex; 239 } 240 finally 241 { 242 if (dateReader != null && !dateReader.IsClosed) 243 { 244 dateReader.Close(); 245 } 246 } 247 } 248 249 #endregion 250 251 #region ExecuteScalar 252 253 public object ExecuteScalar(string cmdText) 254 { 255 return ExecuteScalar(CommandType.Text, cmdText, null); 256 } 257 258 public object ExecuteScalar(CommandType cmdType, string cmdText) 259 { 260 return ExecuteScalar(cmdType, cmdText, null); 261 } 262 263 public object ExecuteScalar(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 264 { 265 266 try 267 { 268 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 269 270 BeforeExecute(cmdText); 271 object val = dbo.ExecuteScalar(SQLiteCommand); 272 SQLiteCommand.Parameters.Clear(); 273 OnSuccess(cmdText); 274 275 return val; 276 } 277 catch (Exception ex) 278 { 279 OnError(cmdText, ex); 280 throw ex; 281 } 282 } 283 284 public object ExecuteScalar(CommandType cmdType, string cmdText, int commandTimeout, params SQLiteParameter[] commandParameters) 285 { 286 try 287 { 288 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 289 SQLiteCommand.CommandTimeout = commandTimeout; 290 291 BeforeExecute(cmdText); 292 object val = dbo.ExecuteScalar(SQLiteCommand); 293 SQLiteCommand.Parameters.Clear(); 294 OnSuccess(cmdText); 295 296 return val; 297 } 298 catch (Exception ex) 299 { 300 OnError(cmdText, ex); 301 throw ex; 302 } 303 } 304 305 public object ExecuteScalar(SQLiteTransaction trans, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 306 { 307 try 308 { 309 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 310 311 BeforeExecute(cmdText); 312 object val = dbo.ExecuteScalar(SQLiteCommand, trans); 313 SQLiteCommand.Parameters.Clear(); 314 OnSuccess(cmdText); 315 316 return val; 317 } 318 catch (Exception ex) 319 { 320 OnError(cmdText, ex); 321 throw ex; 322 } 323 } 324 325 #endregion 326 327 #region ExeSql 328 329 public bool ExeSql(string cmdText) 330 { 331 return ExeSql(CommandType.Text, cmdText, null); 332 } 333 334 public bool ExeSql(CommandType cmdType, string cmdText) 335 { 336 return ExeSql(cmdType, cmdText, null); 337 } 338 339 public bool ExeSql(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 340 { 341 SQLiteConnection conn = (SQLiteConnection)dbo.CreateConnection(); 342 using (conn) 343 { 344 conn.Open(); 345 SQLiteTransaction trans = conn.BeginTransaction(); 346 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 347 try 348 { 349 BeforeExecute(cmdText); 350 dbo.ExecuteNonQuery(SQLiteCommand, trans); 351 trans.Commit(); 352 OnSuccess(cmdText); 353 return true; 354 } 355 catch (Exception ex) 356 { 357 OnError(cmdText, ex); 358 trans.Rollback(); 359 return false; 360 } 361 finally 362 { 363 conn.Close(); 364 } 365 } 366 } 367 368 #endregion ExeSql 369 370 #region GetDataSet 371 372 public DataSet GetDataSet(string cmdText) 373 { 374 return GetDataSet(CommandType.Text, cmdText, null); 375 } 376 377 public DataSet GetDataSet(CommandType cmdType, string cmdText) 378 { 379 return GetDataSet(cmdType, cmdText, null); 380 } 381 382 public DataSet GetDataSet(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 383 { 384 try 385 { 386 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 387 388 BeforeExecute(cmdText); 389 DataSet val = dbo.ExecuteDataSet(SQLiteCommand); 390 OnSuccess(cmdText); 391 392 if (val != null && val.Tables.Count > 0) 393 return val; 394 else 395 return null; 396 } 397 catch (Exception ex) 398 { 399 OnError(cmdText, ex); 400 throw ex; 401 } 402 } 403 404 public DataSet GetDataSet(SQLiteTransaction trans, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 405 { 406 try 407 { 408 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 409 410 BeforeExecute(cmdText); 411 DataSet val = dbo.ExecuteDataSet(SQLiteCommand, trans); 412 OnSuccess(cmdText); 413 414 if (val != null && val.Tables.Count > 0) 415 return val; 416 else 417 return null; 418 } 419 catch (Exception ex) 420 { 421 OnError(cmdText, ex); 422 throw ex; 423 } 424 } 425 426 public DataSet GetDataSet(SQLiteTransaction trans, CommandType cmdType, string cmdText, int commandTimeout, params SQLiteParameter[] commandParameters) 427 { 428 try 429 { 430 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 431 SQLiteCommand.CommandTimeout = commandTimeout; 432 433 BeforeExecute(cmdText); 434 DataSet val = dbo.ExecuteDataSet(SQLiteCommand, trans); 435 OnSuccess(cmdText); 436 437 if (val != null && val.Tables.Count > 0) 438 return val; 439 else 440 return null; 441 } 442 catch (Exception ex) 443 { 444 OnError(cmdText, ex); 445 throw ex; 446 } 447 } 448 449 #endregion 450 451 #region GetDataTable 452 453 public DataTable GetDataTable(string cmdText) 454 { 455 return GetDataTable(CommandType.Text, cmdText, null); 456 } 457 458 public DataTable GetDataTable(CommandType cmdType, string cmdText) 459 { 460 return GetDataTable(cmdType, cmdText, null); 461 } 462 463 public DataTable GetDataTable(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 464 { 465 try 466 { 467 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 468 469 BeforeExecute(cmdText); 470 DataSet val = dbo.ExecuteDataSet(SQLiteCommand); 471 OnSuccess(cmdText); 472 473 if (val != null && val.Tables.Count > 0) 474 return val.Tables[0]; 475 else 476 return null; 477 } 478 catch (Exception ex) 479 { 480 OnError(cmdText, ex); 481 482 throw ex; 483 } 484 } 485 486 public DataTable GetDataTable(CommandType cmdType, string cmdText, int commandTimeout, params SQLiteParameter[] commandParameters) 487 { 488 try 489 { 490 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 491 SQLiteCommand.CommandTimeout = commandTimeout; 492 493 BeforeExecute(cmdText); 494 DataSet val = dbo.ExecuteDataSet(SQLiteCommand); 495 OnSuccess(cmdText); 496 497 if (val != null && val.Tables.Count > 0) 498 return val.Tables[0]; 499 else 500 return null; 501 } 502 catch (Exception ex) 503 { 504 OnError(cmdText, ex); 505 throw ex; 506 } 507 } 508 509 public DataTable GetDataTable(SQLiteTransaction trans, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 510 { 511 try 512 { 513 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 514 515 BeforeExecute(cmdText); 516 DataSet val = dbo.ExecuteDataSet(SQLiteCommand, trans); 517 OnSuccess(cmdText); 518 519 if (val != null && val.Tables.Count > 0) 520 return val.Tables[0]; 521 else 522 return null; 523 } 524 catch (Exception ex) 525 { 526 OnError(cmdText, ex); 527 throw ex; 528 } 529 } 530 531 public DataTable GetDataTable(CommandType cmdType, string cmdText, int index, int pageCount, params SQLiteParameter[] commandParameters) 532 { 533 try 534 { 535 536 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 537 DataTable table = new DataTable(); 538 539 BeforeExecute(cmdText); 540 541 SQLiteDataAdapter adapter = (SQLiteDataAdapter)dbo.GetDataAdapter(); 542 543 using (SQLiteConnection conn = (SQLiteConnection)dbo.CreateConnection()) 544 { 545 SQLiteCommand.Connection = conn; 546 adapter.SelectCommand = SQLiteCommand; 547 548 //取得分页数据 549 int start = (index - 1) * pageCount; 550 551 adapter.Fill(start, pageCount, table); 552 553 if (conn.State != ConnectionState.Closed) 554 { 555 conn.Close(); 556 } 557 } 558 559 OnSuccess(cmdText); 560 561 return table; 562 } 563 catch (Exception ex) 564 { 565 OnError(cmdText, ex); 566 throw ex; 567 } 568 } 569 570 #endregion 571 572 #region GetDataRow 573 574 public DataRow GetDataRow(string cmdText) 575 { 576 return GetDataRow(CommandType.Text, cmdText, null); 577 } 578 579 public DataRow GetDataRow(CommandType cmdType, string cmdText) 580 { 581 return GetDataRow(cmdType, cmdText, null); 582 } 583 584 public DataRow GetDataRow(CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 585 { 586 try 587 { 588 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 589 590 BeforeExecute(cmdText); 591 DataSet val = dbo.ExecuteDataSet(SQLiteCommand); 592 OnSuccess(cmdText); 593 594 if (val != null && val.Tables.Count > 0 && val.Tables[0].Rows.Count > 0) 595 return val.Tables[0].Rows[0]; 596 else 597 return null; 598 } 599 catch (Exception ex) 600 { 601 OnError(cmdText, ex); 602 throw ex; 603 } 604 } 605 606 public DataRow GetDataRow(CommandType cmdType, string cmdText, int commandTimeout, params SQLiteParameter[] commandParameters) 607 { 608 try 609 { 610 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 611 SQLiteCommand.CommandTimeout = commandTimeout; 612 613 BeforeExecute(cmdText); 614 DataSet val = dbo.ExecuteDataSet(SQLiteCommand); 615 OnSuccess(cmdText); 616 617 if (val != null && val.Tables.Count > 0 && val.Tables[0].Rows.Count > 0) 618 return val.Tables[0].Rows[0]; 619 else 620 return null; 621 } 622 catch (Exception ex) 623 { 624 OnError(cmdText, ex); 625 throw ex; 626 } 627 } 628 629 public DataRow GetDataRow(SQLiteTransaction trans, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters) 630 { 631 try 632 { 633 SQLiteCommand SQLiteCommand = GetCommand(dbo, commandParameters, cmdText, cmdType); 634 635 BeforeExecute(cmdText); 636 DataSet val = dbo.ExecuteDataSet(SQLiteCommand, trans); 637 OnSuccess(cmdText); 638 639 if (val != null && val.Tables.Count > 0 && val.Tables[0].Rows.Count > 0) 640 return val.Tables[0].Rows[0]; 641 else 642 return null; 643 } 644 catch (Exception ex) 645 { 646 OnError(cmdText, ex); 647 throw ex; 648 } 649 } 650 651 #endregion 652 653 #region Test 654 655 public bool TestConnection() 656 { 657 try 658 { 659 using (SQLiteConnection conn = (SQLiteConnection)dbo.CreateConnection()) 660 { 661 conn.Open(); 662 663 conn.Close(); 664 665 return true; 666 } 667 } 668 catch (Exception ex) 669 { 670 OnError("Test DataBase Connection.", ex); 671 return false; 672 } 673 } 674 675 #endregion 676 677 678 #region 私有的帮助方法 679 680 /// <summary> 681 /// 初始化数据库对象 682 /// </summary> 683 /// <param name="dbName">数据库名称</param> 684 /// <returns>数据库对象</returns> 685 private Database InitDatabase(string dbName) 686 { 687 //http://entlib.codeplex.com/discussions/442089 688 IConfigurationSource configurationSource = ConfigurationSourceFactory.Create(); 689 690 DatabaseProviderFactory dbFact = new DatabaseProviderFactory(configurationSource); 691 DatabaseFactory.SetDatabaseProviderFactory(dbFact); 692 693 Database dbo = null; 694 if (string.IsNullOrEmpty(dbName)) 695 { 696 dbo = DatabaseFactory.CreateDatabase(); 697 } 698 else 699 { 700 dbo = DatabaseFactory.CreateDatabase(dbName); 701 } 702 return dbo; 703 } 704 705 /// <summary> 706 /// 组装数据库命令对象 707 /// </summary> 708 /// <param name="dbo">数据库</param> 709 /// <param name="commandParameters">命令参数</param> 710 /// <param name="cmdText">数据库命令</param> 711 /// <param name="type">命令类型</param> 712 /// <returns>数据库命令对象</returns> 713 private SQLiteCommand GetCommand(Database dbo, SQLiteParameter[] commandParameters, string cmdText, CommandType type) 714 { 715 SQLiteCommand command = null; 716 717 if (type == CommandType.Text) 718 { 719 command = (SQLiteCommand)dbo.GetSqlStringCommand(cmdText); 720 } 721 else 722 { 723 command = (SQLiteCommand)dbo.GetStoredProcCommand(cmdText); 724 } 725 726 if (commandParameters != null) 727 { 728 for (int i = 0; i < commandParameters.Length; i++) 729 { 730 command.Parameters.Add(commandParameters[i]); 731 } 732 } 733 734 return command; 735 } 736 737 /// <summary> 738 /// 执行前日志 739 /// </summary> 740 /// <param name="cmdText">数据库命令</param> 741 private void BeforeExecute(string cmdText) 742 { 743 //logger.Debug(BEFORE + cmdText); 744 } 745 746 /// <summary> 747 /// 执行成功日志 748 /// </summary> 749 /// <param name="cmdText">数据库命令</param> 750 private void OnSuccess(string cmdText) 751 { 752 //logger.Debug(SUCCESS + cmdText); 753 } 754 755 /// <summary> 756 /// 执行失败日志 757 /// </summary> 758 /// <param name="cmdText">数据库命令</param> 759 /// <param name="ex">异常</param> 760 private void OnError(string cmdText, Exception ex) 761 { 762 //logger.Error(ERROR + cmdText, ex); 763 764 } 765 766 #endregion 767 768 } 769 770 /// <summary> 771 /// 数据访问组件实例化工厂 772 /// </summary> 773 public class DataHelperFactory 774 { 775 private static Dictionary<string, DataHelper> DBPool = new Dictionary<string, DataHelper>(); 776 private static object poolLock = new object(); 777 778 private DataHelperFactory() 779 { } 780 781 public static DataHelper Create(string dbName) 782 { 783 DataHelper instance = null; 784 785 //第一次判断是为了提高效率避免频繁lock 786 if (!DBPool.ContainsKey(dbName)) 787 { 788 lock (poolLock) 789 { 790 if (!DBPool.ContainsKey(dbName)) 791 { 792 instance = new DataHelper(dbName); 793 DBPool.Add(dbName, instance); 794 } 795 } 796 } 797 798 return DBPool[dbName]; 799 } 800 } 801 }
使用示例:
1 public User GetUser(string account) 2 { 3 DataHelper dbo = DataHelperFactory.Create("PeigaoDB"); 4 User user = new User(); 5 var paras = new SQLiteParameter[] 6 { 7 new SQLiteParameter("@Account", account) 8 }; 9 10 DataRow dr = dbo.GetDataRow(CommandType.Text, "SELECT * FROM User WHERE Account = @Account", paras); 11 if(dr != null) 12 { 13 user.Code = Convert.ToInt32(dr["Code"]); 14 user.Account = dr["Account"].ToString(); 15 user.Md5Password = dr["Password"].ToString(); 16 user.CreateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Convert.ToInt32(dr["CreateTime"])); 17 } 18 19 return user; 20 }
使用起来,和使用MSSQL没什么区别。
如果提示provider相关的错误,检查配置文件和代码中的命名空间。
3.申明:代码来自于网络,本程序为Demo,请小心使用。
浙公网安备 33010602011771号