C # SQLite Operation method Summary _c# Tutorial

Source: Internet
Author: User
Tags sqlite create database

This article analyzes the C # SQLite operation method. Share to everyone for your reference, specific as follows:

Recent project needs to save some data in C #, so let's summarize. Need to download SQLite library SourceForge link URL http://sourceforge.net/projects/sqlite-dotnet2/or to the official network http://www.sqlite.org/ Download.html downloads are available and installed after downloading. Add a reference to the C # project to introduce the System.Data.SQLite.dll in the installation directory bin. Add a namespace using System.Data.SQLite; can be in your project

The SQLite of the miniature database for the expansion platform is used,

The main point to note is:

The database is used if it is not created:

Sqliteconnection.createfile (DatabaseName);

The database has been created and is to be accessed:

Copy Code code as follows:
Sqliteconnection m_conn = new Sqliteconnection ("datasource=" +m_dbname+; version=3; New=false; Compress=true; ");

The following is an operational database code that is encapsulated in the project and can be used in a project with a slight modification.

 using System; using System.Collections.Generic; using System.Text; using System.Data.
SQLite;
    Namespace Toolbar {public class Cspdatabase {protected string m_dbname;
    protected string M_tablename;
    protected string M_password;
      Public Cspdatabase (String dbname) {m_dbname = dbname;
      M_tablename = "Mhtinfo";
    M_password = "";  //create DataBase public virtual void Init () {} public virtual void CreateDatabase () {} void OpenDatabase () {} public virtual void SetPassword (string password) {}//connect DataBase public Virtua l void Connectdatabase () {}//create Table public virtual void CreateTable (String tablename) {}//insert Dat A public virtual void Insert (String mhtlocation) {}}} 
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Data.SQLite;
Using System.Windows.Forms;
    Namespace Toolbar {class Sqlitedatabase:cspdatabase {private sqliteconnection m_conn= null;
    Private Sqlitecommand M_cmd=null; Public Sqlitedatabase (String dbname): Base (dbname) {} public override void Init () {if (M_conn = = nul
      L) M_conn = new Sqliteconnection ();
      M_cmd = new Sqlitecommand (); M_cmd.
    Connection = M_conn; public override void CreateDatabase () {//create Database try {sqliteconnection.creat
        Efile (M_dbname);
        Init ();
      Connectdatabase ();
      catch (System.Exception e) {MessageBox.Show ("Create DataBase failed!"); The public override void OpenDatabase () {m_conn = new Sqliteconnection ("Data source=" +m_dbname+; version=3; New=false;
      Compress=true; ");
      Init ();
    Connectdatabase (); } PUBlic override void SetPassword (string password) {m_password = password; override void Connectdatabase () {//connect to DataBase try {sqliteconnections
        Tringbuilder connstr = new Sqliteconnectionstringbuilder (); ConnStr.
        DataSource = M_dbname; if (M_password!= "") connstr.
        Password = M_password; M_conn. ConnectionString = ConnStr.
      ToString ();
      catch (System.Exception e) {MessageBox.Show ("Fail to Connect to the" database); }//create Table public override void CreateTable (string tablename) {try {M_tablen
        AME = tablename; M_conn.
        Open ();
        String sql = "CREATE TABLE" + tablename + "(mhtlocation varchar (20))";
        M_cmd.commandtext = SQL; M_cmd.
        ExecuteNonQuery (); M_conn.
      Close ();
      catch (System.Exception e) {MessageBox.Show ("Create Table failed!"); }} Public override void Insert (string mhtlocation) {try {//insert Data m_conn.
        Open ();
        String sql = "INSERT INTO [" + M_tablename + "] values (' + mhtlocation + ')";
        M_cmd.commandtext = SQL; M_cmd.
        ExecuteNonQuery (); M_conn.
      Close ();
      catch (System.Exception e) {MessageBox.Show (e.tostring ());

 }
    }
  }
}

Read more about C # Interested readers can view the site topics: "C # Programming Thread Usage Tips summary", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # tutorial on data structure and algorithms, summary of C # array manipulation techniques, and an introductory course on C # object-oriented programming

I hope this article will help you with C # programming.

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.