C# SQlite操作方法小結_C#教程

來源:互聯網
上載者:User

本文執行個體分析了C# SQlite操作方法。分享給大家供大家參考,具體如下:

最近項目需求用C#儲存一些資料,如此先總結一下。需要下載Sqlite 庫 SourceForge 連結網址http://sourceforge.net/projects/sqlite-dotnet2/或到官方網http://www.sqlite.org/download.html下載都可以,下載之後安裝。在C#項目中添加引用 引入安裝目錄bin中的System.Data.SQLite.dll。添加命名空間using System.Data.SQLite;便可以在你的項目中

對擴平台的微型資料庫SQlite 進行使用了,

主要注意一點是:

資料庫若未建立則使用:

SQLiteConnection.CreateFile(databaseName);

資料庫已經建立,並要進行訪問:

複製代碼 代碼如下:
SQLiteConnection m_conn = new SQLiteConnection("DataSource="+m_dbName+";Version=3;New=False;Compress=True;");

下面是項目中封裝的操作資料庫代碼,使用時可稍微修改便可在項目中使用。

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

更多關於C#相關內容感興趣的讀者可查看本站專題:《C#程式設計之線程提示總結》、《C#操作Excel技巧總結》、《C#中XML檔案操作技巧匯總》、《C#常見控制項用法教程》、《WinForm控制項用法總結》、《C#資料結構與演算法教程》、《C#數組操作技巧總結》及《C#物件導向程式設計入門教程》

希望本文所述對大家C#程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.