原文鏈接
https://www.cnblogs.com/zhaoliankun/p/9088200.html
我的環境配置:windows 64,VS,SQLite(點擊下載),System.Data.SQLite.DLL(點擊下載)。
1.在VS中新建一個控制台應用程序,如下圖
2.添加引用
將下載的System.Data.SQLite.DLL復制到新建項目的路徑下
在VS中找到項目,右鍵選擇添加引用
瀏覽到dll路徑下,添加進來。
代碼中添加
using System.Data.SQLite;
添加類庫CSQLiteHelper,用於存放SQLite操作方法(此代碼原文鏈接. https://blog.csdn.net/pukuimin1226/article/details/8516733)
具體代碼
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.SQLite; 6 using System.Data; 7 using System.Xml; 8 using System.Text.RegularExpressions; 9 using System.IO; 10 11 namespace CSharp_SQLite 12 { 13 public class CSQLiteHelper 14 { 15 private string _dbName = ""; 16 private SQLiteConnection _SQLiteConn = null; //連接對象 17 private SQLiteTransaction _SQLiteTrans = null; //事務對象 18 private bool _IsRunTrans = false; //事務運行標識 19 private string _SQLiteConnString = null; //連接字符串 20 private bool _AutoCommit = false; //事務自動提交標識 21 22 public string SQLiteConnString 23 { 24 set { this._SQLiteConnString = value; } 25 get { return this._SQLiteConnString; } 26 } 27 28 public CSQLiteHelper(string dbPath) 29 { 30 this._dbName = dbPath; 31 this._SQLiteConnString = "Data Source=" + dbPath; 32 } 33 34 /// <summary> 35 /// 新建數據庫文件 36 /// </summary> 37 /// <param name="dbPath">數據庫文件路徑及名稱</param> 38 /// <returns>新建成功,返回true,否則返回false</returns> 39 static public Boolean NewDbFile(string dbPath) 40 { 41 try 42 { 43 SQLiteConnection.CreateFile(dbPath); 44 return true; 45 } 46 catch (Exception ex) 47 { 48 throw new Exception("新建數據庫文件" + dbPath + "失敗:" + ex.Message); 49 } 50 } 51 52 53 /// <summary> 54 /// 創建表 55 /// </summary> 56 /// <param name="dbPath">指定數據庫文件</param> 57 /// <param name="tableName">表名稱</param> 58 static public void NewTable(string dbPath, string tableName) 59 { 60 61 SQLiteConnection sqliteConn = new SQLiteConnection("data source=" + dbPath); 62 if (sqliteConn.State != System.Data.ConnectionState.Open) 63 { 64 sqliteConn.Open(); 65 SQLiteCommand cmd = new SQLiteCommand(); 66 cmd.Connection = sqliteConn; 67 cmd.CommandText = "CREATE TABLE " + tableName + "(Name varchar,Team varchar, Number varchar)"; 68 cmd.ExecuteNonQuery(); 69 } 70 sqliteConn.Close(); 71 } 72 /// <summary> 73 /// 打開當前數據庫的連接 74 /// </summary> 75 /// <returns></returns> 76 public Boolean OpenDb() 77 { 78 try 79 { 80 this._SQLiteConn = new SQLiteConnection(this._SQLiteConnString); 81 this._SQLiteConn.Open(); 82 return true; 83 } 84 catch (Exception ex) 85 { 86 throw new Exception("打開數據庫:" + _dbName + "的連接失敗:" + ex.Message); 87 } 88 } 89 90 /// <summary> 91 /// 打開指定數據庫的連接 92 /// </summary> 93 /// <param name="dbPath">數據庫路徑</param> 94 /// <returns></returns> 95 public Boolean OpenDb(string dbPath) 96 { 97 try 98 { 99 string sqliteConnString = "Data Source=" + dbPath; 100 101 this._SQLiteConn = new SQLiteConnection(sqliteConnString); 102 this._dbName = dbPath; 103 this._SQLiteConnString = sqliteConnString; 104 this._SQLiteConn.Open(); 105 return true; 106 } 107 catch (Exception ex) 108 { 109 throw new Exception("打開數據庫:" + dbPath + "的連接失敗:" + ex.Message); 110 } 111 } 112 113 /// <summary> 114 /// 關閉數據庫連接 115 /// </summary> 116 public void CloseDb() 117 { 118 if (this._SQLiteConn != null && this._SQLiteConn.State != ConnectionState.Closed) 119 { 120 if (this._IsRunTrans && this._AutoCommit) 121 { 122 this.Commit(); 123 } 124 this._SQLiteConn.Close(); 125 this._SQLiteConn = null; 126 } 127 } 128 129 /// <summary> 130 /// 開始數據庫事務 131 /// </summary> 132 public void BeginTransaction() 133 { 134 this._SQLiteConn.BeginTransaction(); 135 this._IsRunTrans = true; 136 } 137 138 /// <summary> 139 /// 開始數據庫事務 140 /// </summary> 141 /// <param name="isoLevel">事務鎖級別</param> 142 public void BeginTransaction(IsolationLevel isoLevel) 143 { 144 this._SQLiteConn.BeginTransaction(isoLevel); 145 this._IsRunTrans = true; 146 } 147 148 /// <summary> 149 /// 提交當前掛起的事務 150 /// </summary> 151 public void Commit() 152 { 153 if (this._IsRunTrans) 154 { 155 this._SQLiteTrans.Commit(); 156 this._IsRunTrans = false; 157 } 158 } 159 160 161 } 162 }
此時運行會報錯,
警告 所生成項目的處理器架構“MSIL”與引用“System.Data.SQLite”的處理器架構“x86”不匹配。這種不匹配可能會導致運行時失敗。請考慮通過配置管理器更改您的項目的目標處理器架構,以使您的項目與引用間的處理器架構保持一致,或者為引用關聯一個與您的項目的目標處理器架構相符的處理器架構。
修改項目屬性:x86。
再次運行,無誤。