SQLite安裝和調用


  想把項目的SQL SERVER數據庫換掉,因為SQL SERVER過於龐大,而我的項目只是小型的桌面應用程序。

  網上搜了一下,發現了SQLite,真是個好東西,無需安裝和部署,關鍵是客戶電腦什么都不用裝就能使用數據庫,大小只有1M多,正是我想要的。

  花了半天時間,把項目的數據庫替換掉,SQL語句基本都能用,只修改了個別語句。把SQLite的用法做個簡單的記錄。

  一、SQLite安裝

  下載地址:http://www.sqlite.org/download.html 我用的是win7 32位,選了這兩個。

  下載解壓后,將兩個加壓后的文件放入C盤新建的sqlite文件夾,或者其他任意的文件夾,作為SQLite的安裝路徑。

  如果使用dos環境配置數據庫和表結構,需要用到sqlite3這個命令,需要將sqlite3.exe配置到系統環境變量,右擊計算機->屬性->高級系統設置->環境變量,編輯Path,添加C:\sqlite,

即我們建立的安裝路徑。

添加后,可以使用cmd,輸入sqlite3,出現

  即為添加成功,然后我們可以在dos窗口下使用命令創建數據庫和表,包括各種增刪改查操作。如果不喜歡dos操作,就不用添加環境變量,直接使用可視化工具。

  二、SQLite可視化工具

  我個人喜歡可視化操作,雖然沒有dos操作“高大上”,但是有方便的工具為啥不用呢?我搜了一下,SQLite的可視化工具還挺多,我下了一款,叫SQLite Studio,感覺還可以。

  SQLite Studio下載地址:https://sqlitestudio.pl/index.rvt 

  在可視化工具下,傻瓜式創建數據庫和表。

  三、SQLite驅動

  數據庫創建好后,需要程序連接數據庫,因為我用的是C#,所以還要下載C# 32位的驅動程序。

  驅動下載地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki ,我習慣用.net 4.0,所以下載了這個

  下載后直接安裝,然后在程序里引用安裝路徑下的System.Data.SQLite.dll,導入命名空間:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite; namespace DAL
{
    public class DAL_LOGIN
    {
        private static string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;      System.Data.DataSet ds = new System.Data.DataSet();
        SQLiteConnection sqlConn = new SQLiteConnection();
        SQLiteCommand sqlComm = new SQLiteCommand();
        SQLiteDataAdapter sqlDa = new SQLiteDataAdapter();
        SQLiteCommandBuilder scb = new SQLiteCommandBuilder();

        public void Conn()
        {
            if(sqlConn.State == ConnectionState.Closed)
            {
                sqlConn = new SQLiteConnection(conStr);
                sqlConn.Open();
                sqlComm.Connection = sqlConn;
                sqlComm.CommandType = CommandType.Text;
            }

        }

        public void Close()
        {
            sqlDa.Dispose();
            sqlDa = null;
            sqlComm.Dispose();
            sqlComm = null;
            sqlConn.Dispose();
            sqlConn = null;
        }

        public DataTable GetUserName()
        {        
            sqlComm.CommandText="select distinct UserName from CMSUSERINFO order by UserName asc";
            sqlComm.Parameters.Clear();

            sqlDa.SelectCommand = sqlComm;        
            sqlDa.Fill(ds, "MER_LOGIN_USERNAME");
            ds.Tables["MER_LOGIN_USERNAME"].Clear();
            sqlDa.Fill(ds, "MER_LOGIN_USERNAME");

            return ds.Tables["MER_LOGIN_USERNAME"];
        }
        public int CheckUserInfo(string name,string pass)
        {
                sqlComm.CommandText = "select count(*) from CMSUSERINFO where UserName = @name and UserPass =@pass";
                sqlComm.Parameters.Clear();
                var paras = new SQLiteParameter[] { new SQLiteParameter("@name", name), new SQLiteParameter("@pass", pass) };
                sqlComm.Parameters.AddRange(paras);
                int n =int.Parse(sqlComm.ExecuteScalar().ToString());
                return n;
        }

    }

}

  使用App.config配置連接字符串:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="SQLConnectionString" 
         connectionString="Data Source=|DataDirectory|\*.db;Pooling=true;FailIfMissing=false"
         providerName="System.Data.SQLite"/>
  </connectionStrings>
</configuration>

  我喜歡把*.db放入到應用程序路徑下,直接拷貝就能在其他電腦上運行。在客戶電腦文件夾內只需要System.Data.SQLite.dll和*.db這兩個文件即可訪問數據庫,當然.net 4.0是必需的。

 
        
 
 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM