程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# SQlite操作辦法小結

C# SQlite操作辦法小結

編輯:C#入門知識

C# SQlite操作辦法小結。本站提示廣大學習愛好者:(C# SQlite操作辦法小結)文章只能為提供參考,不一定能成為您想要的結果。以下是C# SQlite操作辦法小結正文


本文實例剖析了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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved