VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之整理两个PetaPoco连接SQLite数据库的方法

  1. 从https://github.com/qingask/PetaPoco.NetCore下载源文件压缩包
  2. 解压出文件PetaPoco.Multiple.cs、PetaPoco.NetCore.cs
  3. 放置System.Data.SQLite.dll文件到bin文件,这个需要从SQLite官方获取对应框架集级对应位文件,应该可以直接通过NuGet获取。
  4. 方法1:
    1. 增加DbProviderFactories类,向PetaPoco注入SQLite实例
      复制代码
      public static class DbProviderFactories
      {
      
          internal static readonly Dictionary<string, DbProviderFactory> Configs = new Dictionary<string, DbProviderFactory>();
      
          static DbProviderFactories()
          {
              RegisterFactory("Microsoft.Data.Sqlite", SQLiteFactory.Instance);
          }
      
          public static DbProviderFactory GetFactory(string providerInvariantName)
          {
              if (Configs.ContainsKey(providerInvariantName))
              {
                  return Configs[providerInvariantName];
              }
              return null;
          }
      
          public static void RegisterFactory(string providerInvariantName, DbProviderFactory factory)
          {
              if (Configs.ContainsKey(providerInvariantName))
              {
                  Configs[providerInvariantName] = factory;
              }
              else
              {
                  Configs.Add(providerInvariantName, factory);
              }
          }
      
          public static IEnumerable<string> GetFactoryProviderNames()
          {
              return Configs.Keys.ToArray();
          }
      }
      复制代码

       

    2. 配置web.config
      <add name="PetaPocoConn" connectionString="Data Source=|DataDirectory|Demo.db"/>

       

    3. 链接语法 
      var db=new Database(new SQLiteConnection(ConfigurationManager.ConnectionStrings["PetaPocoConn"].ConnectionString));

       

  5.   方法2:
    1.   配置web.config 
      复制代码
       <system.data>
          <DbProviderFactories>
            <remove invariant="System.Data.SQLite"/>         
            <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /> 
          </DbProviderFactories>
        </system.data>
      复制代码

       

    2. 链接字符串:
      <connectionStrings>
          <add name="PetaPocoConn" connectionString="Data Source=|DataDirectory|Demo.db" providerName="System.Data.SQLite" />
        </connectionStrings>

       

    3. 辅助类:
      复制代码
          public static class DbScoure
          {
              public static Database SqliteDataBase()
              {
                  var conn = ConfigurationManager.ConnectionStrings["PetaPocoConn"];
                  return new Database(conn.ConnectionString, conn.ProviderName);
              }
          }
      调用方式:
      var db=DbScoure.SqliteDataBase();
      复制代码

       


相关教程