C # Connect SQLite database method

Source: Internet
Author: User
Tags sqlite sqlite database sqlite server connectionstrings



--Connect with Enterprise Library to operate SQLite



Enterprise Library is one of our commonly used frameworks, can download Enterprise Library 5.0.msi from http://entlib.codeplex.com/. After installation there are documentation for the source code and CHM. Many of the ideas inside are more worthy of our programmers to study.
The data access component in the Enterprise Library is one of our most commonly used data access components. The component supports SQL Server and Oracle database access by default and supports custom extensions.



--Using the Enterprise Library to manipulate the SQLite database
Need to use an extension component of the Enterprise Library Contrib. It expands many of the features of the Enterprise Library. The expansion of the database includes the access operation SQLite, so that we can operate as SQL Server,
Keep the code from being changed so that you can easily transition to SQLite. You can also download to the latest entlibcontrib-5.0.505.0-2011-10-29-bin.zip on http://entlib.codeplex.com/.



--sqlite.net is also a data access component
The System.Data.SQLite is as if the. NET comes with System.Data.SqlClient. It contains connection, command and other data access common objects, but they are preceded by a prefix sqlite.
: http://sqlite.phxsoftware.com/Download the latest version of Sqlite,sqlite-1.0.66.0-setup.exe, The dynamic link library System.Data.SQLite.DLL will be generated when the installation is complete, and the System.Data.SQLite can be referenced directly in the project.
This action is required only if you are accessing SQLite using Sqlite.net.



--sqlite Expert is a Visual database management tool
Allows users to perform operations such as create, edit, copy, extract, etc. on the SQLite server. SQLite Expert supports the SQLite features of all graphical interfaces. It includes a visual query builder, a SQL edit with syntax highlighting and code autocomplete,
Powerful table and view design and import and export capabilities. SQLite Expert is now divided into two versions, one for free personal Edition, and one for a fee Professional Edition.



--Connection method
First add the following configuration in Web. config or App. config, the db of the ConnectionString configuration section is the SQLite database file, put it in the Web app's App_Data directory, | Datadirectory| represents the location of this directory,
The following is the file name, and the rest is that we use the Enterprise Library to access SQL Server is the same.


<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>


--use sqlite.net to access SQLite
After you add a System.Data.SQLite reference. Add the following configuration to the configuration file (Web. config or app. config)
That is to add a dbproviderfactory creation source, in code you can use the DbProviderFactory class to create SQLite data Access objects.


<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>


--Use the original eco-ADO to access SQLite
The original ecological access, that is, directly with connection and command these objects open the database, and then open the connection, the operation of the data.


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]);
                            }
                        }
                    }





C # Connect SQLite database method


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.