C # how to connect to the SQLite Database

Source: Internet
Author: User
Tags sqlite server connectionstrings
  1. IntegrationEnterprise LibraryConnect to and operate SQLite

    Enterprise Library is one of our common frameworks. You can download Enterprise Library 5.0.msi from http://entlib.codeplex.com. After installation, the source code and chm documents are provided. Many of the ideas are worth studying by our programmers.
    The data access component in the enterprise database is also one of our Common Data Access Components. By default, the component supports database access from SQL Server and Oracle, and supports custom extensions.

  2. Use the enterprise database to operate SQLite Databases
    Enterprise Library Contrib is an extension component of the Enterprise Library. It extends many functions of the enterprise database. The expansion of the database includes the access to SQLite, so that we can operate the SQL SERVER,
    The code can be easily transitioned to SQLite without much modification. You can also download the latest entlibcontrib-5.0.505.0-2011-10-29-bin.zipon http://entlib.codeplex.com.

  3. SQLite. NET is also a data access component.
    The System. Data. SQLite is like the System. Data. SqlClient that comes with. NET. It contains common data access objects such as connection and command, but they all have a prefix sqlite.
    : Bytes.
    This operation is required only when SQLite. NET is used to access SQLite.

  4. SQLite Expert is a visual database management tool.
    Allows you to create, edit, copy, and extract data on the SQLite server. SQLite Expert supports SQLite features of all graphical interfaces. It includes a visual query generator, an SQL editing and syntax highlighting, and automatic completion of code,
    Powerful table and view design and import/export functions. SQLite Expert is now divided into two versions: Free Personal Edition and paid Professional Edition.

  5. Connection Method
    First. config or app. add the following configuration to config. In the connectionstring configuration section, the db is the SQLite database file, which is placed in the App_Data directory of the Web application. | DataDirectory | this indicates the location of this directory,
    The file name is followed, and the rest is that we use the Enterprise Library to access 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>


  6. Use SQLite. NET to access SQLite
    Add a reference to System. Data. SQLite. Add the following configuration in the configuration file (web. config or app. config,
    That is, to add a creation source for DbProviderFactory, you can use the DbProviderFactory class in the code to create the SQLite Data Access Object.

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


  7. Access SQLite using original ecology ADO. NET
    Original access means to directly open the database with the connection and command objects, and then open the connection for data operations.

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

     

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.