C#引入SqlLite
作者:互联网
首先在SqlLite的官网中下载SqlLite文件:
官网下载地址:https://www.sqlite.org/download.html
选择这个for .net
然后如下图所示,一般64位的同学下载这个就可以了
下载解压完成后如下图所示:
其中:System.Data.SQLite.dll这个文件添加引用到工程中;
然后注意:SQLite.Interop.dll文件放到编译生成的目录下;
System.Data.SQLite.dll.config中是配置文件的说明,按需放到app.config(客户端软件)或者web.config(服务端软件)中,初学者可以不用管。
同时,VS中对项目进行设置,引用的x64的SqlLite就设置成64的编译环境
示例代码:
using System; using System.Data.SQLite; namespace SQLiteSamples { class Program { //数据库连接 SQLiteConnection m_dbConnection; static void Main(string[] args) { Program p = new Program(); } public Program() { createNewDatabase(); connectToDatabase(); createTable(); fillTable(); printHighscores(); } //创建一个空的数据库 void createNewDatabase() { SQLiteConnection.CreateFile("MyDatabase.sqlite"); } //创建一个连接到指定数据库 void connectToDatabase() { m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;"); m_dbConnection.Open(); } //在指定数据库中创建一个table void createTable() { string sql = "create table highscores (name varchar(20), score int)"; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); } //插入一些数据 void fillTable() { string sql = "insert into highscores (name, score) values ('Me', 3000)"; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); sql = "insert into highscores (name, score) values ('Myself', 6000)"; command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); sql = "insert into highscores (name, score) values ('And I', 9001)"; command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); } //使用sql查询语句,并显示结果 void printHighscores() { string sql = "select * from highscores order by score desc"; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]); Console.ReadLine(); } } }
控制台程序运行结果
运行目录下生成MyDatabase.sqlite数据库文件
标签:dbConnection,C#,SQLiteCommand,command,sql,引入,new,void,SqlLite 来源: https://www.cnblogs.com/LeeSki/p/14381192.html