Summary of C SQlite Operation Methods

  • 2021-10-24 23:28:16
  • OfStack

In this paper, the operation method of C # SQlite is analyzed with examples. Share it for your reference, as follows:

Recent project needs to save some data with C #, so summarize it first. You need to download the Sqlite library SourceForge link website http://sourceforge.net/projects/sqlite-dotnet2/or download it from the official website http://www.sqlite.org/download.html, and install it after downloading. Add a reference in the C # project to introduce System. Data. SQLite. dll in the installation directory bin. Add the namespace using System. Data. SQLite; You can use it in your project

The micro database SQlite of the expanded platform is used,

The main points to note are:

If the database is not created, use:


SQLiteConnection.CreateFile(databaseName);

The database has been created and will be accessed:

SQLiteConnection m_conn = new SQLiteConnection("DataSource="+m_dbName+";Version=3;New=False;Compress=True;");

The following is the operational database code encapsulated in the project, which can be slightly modified to be used in the project.


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() { }
    public virtual void OpenDataBase() { }
    public virtual void SetPassWord(string password) { }
    //Connect DataBase
    public virtual void ConnectDataBase() { }
    //Create Table
    public virtual void CreateTable(string tableName) { }
    //Insert Data
    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 == null)
        m_conn = new SQLiteConnection();
      m_cmd = new SQLiteCommand();
      m_cmd.Connection = m_conn;
    }
    public override void CreateDataBase()
    {
      //Create Database
      try
      {
        SQLiteConnection.CreateFile(m_dbName);
        Init();
        ConnectDataBase();
      }
      catch (System.Exception e)
      {
        MessageBox.Show("Create DataBase Failed!");
      }
    }
    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;
    }
    public override void ConnectDataBase()
    {
      //Connect to DataBase
      try
      {
        SQLiteConnectionStringBuilder 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_tablename = 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());
      }
    }
  }
}

For more readers interested in C # related content, please check the topics on this site: "Summary of Thread Use Skills in C # Programming", "Summary of C # Operating Excel Skills", "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: