sqlite 数据库操作 创建数据库
2015-08-18 20:22
253 查看
C#使用SQLite步骤:
(1)新建一个project
(2)添加SQLite操作驱动dll引用
(3)使用API操作SQLite DataBase
(1)新建一个project
(2)添加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.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(); } } }
相关文章推荐
- SQLite教程(十一):临时文件
- SQLite中重置自动编号列的方法
- 保护你的Sqlite数据库(SQLite数据库安全秘籍)
- SQLite教程(十):内存数据库和临时数据库
- SQLite 入门教程二 SQLite的创建、修改、删除表
- 详解SQLite中的数据类型
- 详解SQLite中的查询规划器
- C#封装的Sqlite访问类实例
- SQLite中的B-Tree实现细节分析
- SQLite 中文指南之FAQ第1/6页
- SQLite 内存数据库学习手册
- SQLite中的WAL机制详细介绍
- Sqlite数据库里插入数据的条数上限是500
- SQLite速度评测代码
- 简单分析SQLite4的一些设计改变
- C++操作SQLite简明教程
- C#实现Excel导入sqlite的方法
- C#调用SQLite的方法实例分析
- sqlite时间戳转时间语句(时间转时间戳)
- SQLite教程(四):内置函数