原文連結 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。
再次運作,無誤。