zoukankan      html  css  js  c++  java
  • C#连接SQLite数据库方法

    --结合Enterprise Library连接,操作SQLite

      企业库是我们常用的框架之一,可以从http://entlib.codeplex.com/下载Enterprise Library 5.0.msi。安装之后有源代码和chm的文档。里面的很多思想更值得我们程序员去研究。
    企业库中的数据访问组件更是我们常用的数据访问组件之一。组件默认支持SQL Server和Oracle的数据库访问,支持自定义的扩展。

    --使用企业库操作SQLite数据库
    需要用到企业库的一个扩展组件,Enterprise Library Contrib 。里面扩展了企业库的很多功能。其中对数据库的扩展包括了访问操作SQLite,让我们可以像在操作SQL SERVER那样,
    保持代码不用很大的修改,可以很容易的过渡到SQLite上。在http://entlib.codeplex.com/上也可以下载到最新的entlibcontrib-5.0.505.0-2011-10-29-bin.zip。

    --SQLite.NET也是一个数据访问组件
    其中的System.Data.SQLite就好像是.NET自带的System.Data.SqlClient一样。里面包含了connection、command等数据访问的常用对象,只是他们前面都有一个前缀sqlite。
    下载地址: http://sqlite.phxsoftware.com/下载最新版SQLite,SQLite-1.0.66.0-setup.exe,安装完成后会生成动态链接库System.Data.SQLite.DLL,在项目中直接引用System.Data.SQLite即可。
    只有使用SQLite.NET访问SQLite时才需要此操作。

    --SQLite Expert是一款可视化的数据库管理工具
    允许用户在 SQLite 服务器上执行创建、编辑、复制、提取等操作。SQLite Expert支持所有的图形界面的SQLite特征。它包括一个可视化查询生成器,一个SQL编辑与语法突出和代码自动完成,
    强大的table和view设计与导入导出功能。 SQLite Expert现在分为两个版本,一个是免费的Personal Edition,一个是收费 Professional Edition。

    --连接方法
    首先在web.config或者是app.config中添加如下配置,connectionstring配置节的db就是 SQLite的数据库文件,将它放在Web应用的App_Data目录,|DataDirectory|就代表这个目录的位置,
    后面的就是文件名,剩下的就是我们使用企业库访问SQL Server是一样的了。

    <configuration>    
        <configSections>    
          <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, 
    Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null />    
        </configSections>    
        <dataConfiguration defaultDatabase="
           
      ">    
           <providerMappings>    
            <add databaseType="EntLibContrib.Data.SQLite.SQLiteDatabase, EntLibContrib.Data.SqLite, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"    
              name="System.Data.SQLite" />    
          </providerMappings>    
        </dataConfiguration>    
        <connectionStrings>    
          <add name="sqlite" connectionString="Data Source=|DataDirectory|db;Pooling=true;FailIfMissing=false"    
            providerName="System.Data.SQLite" />    
        </connectionStrings>    
      </configuration>

    --使用SQLite.NET访问SQLite
    添加System.Data.SQLite的引用之后。在配置文件(web.config or app.config)中添加如下配置,
    也就是添加一个DbProviderFactory的创建源,在代码中就可以使用DbProviderFactory类来创建SQLite的数据访问对象了。

    <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, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />          
    </DbProviderFactories>        
    </system.data>

    --使用原生态的ADO.NET访问SQLite
    原生态的访问,就是说直接用connection和command这些对象打开数据库,然后打开连接,进行数据的操作。

    DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite");
                        using (DbConnection conn = fact.CreateConnection())
                        {
                            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlite"].ConnectionString;
                            conn.Open();
                            DbCommand comm = conn.CreateCommand();
                            comm.CommandText ="select * from customer";
                            comm.CommandType = CommandType.Text;
                            using (IDataReader reader = comm.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    Response.Write(reader[0]);
                                }
                            }
                        }
  • 相关阅读:
    Vue路由机制
    谷歌浏览器打不开应用商店的解决方法
    Vue报错——Component template should contain exactly one root element. If you are using vif on multiple elements, use velseif to chain them instead.
    Vue.js学习之——安装
    Vue使用axios无法读取data的解决办法
    关于localstorage存储JSON对象的问题
    2013年整体计划
    个人喜欢的警语收集
    Linux防火墙的关闭和开启
    Flex修改title 转载
  • 原文地址:https://www.cnblogs.com/dekevin/p/4569402.html
Copyright © 2011-2022 走看看