標籤:ram net 操作 工具 UI apt param 環境 cms
想把項目的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 [email protected]"; 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是必需的。
SQLite安裝和調用