VS中C#连接SQLite数据库处理器架构“x86”不匹配的问题
原文链接
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。
再次运行,无误。