--首發於博客園, 轉載請保留此鏈接 博客原文地址
本文運行環境: Win7 X64, VS2010
1. SQLite 的優點:
SQLite 是一款輕型數據庫,開發包只有十幾M, 相對於 MSSQL 幾個 G 的體積來說簡直是小 Case, 而且在打包成的軟件里只需要添加相關的 DLL 就可以在其他的電腦運行,這一點來說比 Access 數據庫還要來得方便。
SQLite雖然很小巧,但是支持的SQL語句不會太遜色於其他開源數據庫。
更多詳情參見百科:SQLite
2. 開發包下載
本文所用開發包:sqlite-netFx40-setup-bundle-x86-2010-1.0.93.0.exe
3. VS 2010 使用 SQLite
3.1 安裝 SQLite
3.1.1. 安裝部件有3個選項:Full installation(完全安裝), Compact installation(精簡安裝), Custom installation(自定義安裝), 本文所選為 Full installation
3.1.2 默認為不勾選 Instatll the designer components for Visual Studio 2010, 可以把這個選上
3.1.2 先在本地創建一個空白文件,擴展名為*.db,
添加 SQLite 連接: Server Explorer -> Data Connections -> Add Connection ...
Data Source 為 SQLite Database file
由於表結構一般是一次性創作,所以添加數據庫后使用視圖直接建立數據表結構,不必使用代碼創建
3.1.3 添加 SQLite 引用
Solution Explorer -> Reference -> Add Reference... 找到SQLite 安裝路徑添加 System.Data.SQLite.dll 引用
該類庫包含SQLiteConnection , SQLiteCommand,SQLiteDataAdapter 等連接操作數據庫需要的類
3.1.4 新建 SQLiteHelper 類,主要是db 連接的基本信息,如路徑,以及一些數據表的基本操作方法(根據個人需要):
public class SQLiteHelper { public SQLiteConnection Connection { get { if (_cn == null) { Settings settings = new Settings(); string connection = settings.Connection.ToString(); _cn = new SQLiteConnection(connection); } return _cn; } } private SQLiteConnection _cn = null; public DataTable GetDataTable(string query) { DataTable dt = new DataTable(); try { Connection.Open(); GetAdapter(query).Fill(dt); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { Connection.Close(); } return dt; } public bool Update(string tableName, DataTable dt) { try { Connection.Open(); string tableStructureQuery = string.Format("SELECT * FROM {0} WHERE 1 = 0", tableName); SQLiteDataAdapter da = GetAdapter(tableStructureQuery); da.Update(dt); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { Connection.Close(); } return true; } public SQLiteDataAdapter GetAdapter(string query) { SQLiteCommand selectCommand = new SQLiteCommand(query, Connection); SQLiteDataAdapter adapter = new SQLiteDataAdapter(selectCommand); SQLiteCommandBuilder sb = new SQLiteCommandBuilder(adapter); return adapter; } public bool ExecuteScript(string query) { try { SQLiteCommand selectCommand = new SQLiteCommand(query, Connection); Connection.Open(); selectCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } finally { Connection.Close(); } return true; } } public static class DataViewHelper { public static DataView Clone(this DataView source, string filter) { if (source == null) { throw new ApplicationException("Source cannot be null"); } DataView newView = new DataView(source.Table); string viewfilter = source.RowFilter; List<string> filters = new List<string>(); if (!string.IsNullOrEmpty(viewfilter)) filters.Add(string.Format("({0})", viewfilter)); if (!string.IsNullOrEmpty(filter)) filters.Add(string.Format("({0})", filter)); newView.RowFilter = string.Join(" AND ", filters.ToArray()); return newView; } public static DataView Clone(this DataView source) { return source.Clone(string.Empty); } }
(1) 其中 Setting.Connection 為 string 類型,只需要指定 db 路徑,本文 Connection 為: Data Source=D:\Trade\Trade\DB\Quotation.db
這里提一下 MSSQL 的 連接字符串以示比較,一般情況下 MSSQL 的連接為ConnectionString 類型的配置節點,如:
<add name="Test" connectionString="Data Source=ServerName;Initial Catalog=dbName;User ID=xxx;Password=***;"
providerName="System.Data.SqlClient" />
(2) 可以看到數庫表的操作方法 與 其他數據庫操作 類似:連接,運行操作命令,關閉
(3) 筆者嘗試添加一個 SQLiteParameter.Direction 為 ParameterDirection.Output 的參數 但報錯,至今沒找到正確的使用方法
(4) 使用 SQLiteConnection 可以用 using 的方式使用,有利於程序內存管理及垃圾回收
3.1.5 新建 DAO 類,繼承 SQLiteHelper ,主要是針對具體表 的方法:
public class DAO : SQLiteHelper { public DAO(string tableName) { TableName = tableName; } public string SelectTableQuery { get { return "SELECT * FROM " + TableName; } } public string TableStructureQuery { get { return SelectTableQuery + " WHERE 1=0"; } } public int GetMaxID(string fieldName) { StringBuilder commandText = new StringBuilder(); commandText.AppendLine(string.Format(@"SELECT MAX({0}) ", fieldName)); commandText.AppendLine(string.Format(@" FROM {0} ", TableName)); DataTable dt = GetDataTable(commandText.ToString()); if (dt == null || dt.Rows.Count == 0) return 0; else return int.Parse(dt.Rows[0][0].ToString()); } public string TableName { get; set; } internal DataTable GetDataTableStructor() { return GetDataTable(TableStructureQuery); } public bool Update(DataTable dt) { return base.Update(TableName, dt); } }
4. SQLite 實例小程序
Quotation 小程序是用於查看工商銀行貴金屬報價的小工具(這里只更新紙黃金報價),工行貴金屬報價官方網址。
用戶可以隨時查看報價,設置 更新頻率(默認更新周期為 120 s )。
下載的報價數據保存到 quotation.db 里,同時用戶可以上傳個人貴金屬賬戶交易記錄。
根據交易記錄設置個人買賣差價,選擇是否播放音樂提醒用戶買入賣出,適用於貴金屬短線投資輔助小工具(投資有風險,入市需謹慎)。
4.1 使用方法:
4.1.1 下載:Quotation Setup 和 db 文件 ( setup 文件用 VS 自帶工具打包好的MSI 文件 非源代碼 )
4.1.2 安裝 Setup 文件, PS: 想要了解 db 結構的讀者可以用 VS 或其他 SQLite 命令、視圖軟件查看
4.1.3 更改配置文件,打開 Trade.exe.config , 修改 節點:
<setting name="Connection" serializeAs="String">
<value>Data Source=D:\Trade\Trade\DB\Quotation.db</value>
</setting>
其中 <value>Data Source = db path</value> 是 Quotation.db 的存放路徑,注意如果軟件安裝在 C 盤保存時可能提示無法訪問,這時可以把 .config 文件復制出來修改后再替換原來的文件
4.1.4 從 <Load Trx> 菜單命令導入從工行下載好的 txt 文件(不要更改格式)用於比較差價, <Transactions> 查看導入的交易記錄,無效的交易記錄可以選中后 Transactions -> Close 來關閉記錄。
4.1.5 小程序為筆者個人開發,用於學習交流,個人免費使用,著作權解釋權歸軟件作者所有,任何人不得進行反編譯及以此向他人收取任何費用,歡迎讀者留言提出意見和建議。
4.1.6 程序截圖
補充:
android app 查詢工行報價可到以下網址下載:
IcbcQuotation 下載 http://pan.baidu.com/s/1mgFsu2c#path=%252FAndroid%2520App%2520Release
軟件說明: 其實是報價網址的快捷方式,只是每隔30s 自動刷新一下
軟件截圖: