SQLite是一个开源、免费的小型RDBMS(关系型数据库),能独立运行、无服务器、零配置、支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准。
这意味着与其他数据库一样,您不需要在系统中配置。SQLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite 直接访问其存储文件。
SQLite 源代码不受版权限制。SQLite数据库官方主页:http://www.sqlite.org/index.html
在 SQLite 中,SQL92 不支持的特性如下所示:
http://www.connectionstrings.com/sqlite/
基本方式:Data Source=c:\mydb.db;Version=3;
驱动dll下载:System.Data.SQLite
也Nuget 直接搜索安装:System.Data.SQLite.Core
C#使用SQLite步骤:
(1)新建一个project
(2)添加SQLite操作驱动dll引用:System.Data.SQLite.dll
(3)使用API操作SQLite DataBase
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.db");//可以不要此句 } //创建一个连接到指定数据库 void connectToDatabase() { m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.db;Version=3;");//没有数据库则自动创建 m_dbConnection.Open(); } //在指定数据库中创建一个table void createTable() { string sql = "create table if not exists 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(); } } }
到此这篇关于C#操作SQLite数据库的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。