C#使用System.Data.SQLite操作SQLite
来源:互联网 发布:编程谜题 百度云 编辑:程序博客网 时间:2024/06/05 22:48
使用System.Data.SQLite
下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
得到System.Data.SQLite.dll添加到工程引用;
建表,插入操作
- static void Main(string[] args)
- {
- SQLiteConnection conn = null;
- string dbPath = "Data Source =" + Environment.CurrentDirectory + "/test.db";
- conn = new SQLiteConnection(dbPath);//创建数据库实例,指定文件位置
- conn.Open();//打开数据库,若文件不存在会自动创建
- string sql = "CREATE TABLE IF NOT EXISTS student(id integer, name varchar(20), sex varchar(2));";//建表语句
- SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);
- cmdCreateTable.ExecuteNonQuery();//如果表不存在,创建数据表
- SQLiteCommand cmdInsert = new SQLiteCommand(conn);
- cmdInsert.CommandText = "INSERT INTO student VALUES(1, '小红', '男')";//插入几条数据
- cmdInsert.ExecuteNonQuery();
- cmdInsert.CommandText = "INSERT INTO student VALUES(2, '小李', '女')";
- cmdInsert.ExecuteNonQuery();
- cmdInsert.CommandText = "INSERT INTO student VALUES(3, '小明', '男')";
- cmdInsert.ExecuteNonQuery();
- conn.Close();
- }
可以使用SQLite Database Browser来查看数据:
下载地址:http://sourceforge.net/projects/sqlitebrowser/
建表成功。
当然这种方法插入数据效率不高,数据量大的话要使用下面这种方法:
- static void Main(string[] args)
- {
- string dbPath = Environment.CurrentDirectory + "/test.db";//指定数据库路径
- using(SQLiteConnection conn = new SQLiteConnection("Data Source =" + dbPath))//创建连接
- {
- conn.Open();//打开连接
- using(SQLiteTransaction tran = conn.BeginTransaction())//实例化一个事务
- {
- for (int i = 0; i < 100000; i++ )
- {
- SQLiteCommand cmd = new SQLiteCommand(conn);//实例化SQL命令
- cmd.Transaction = tran;
- cmd.CommandText = "insert into student values(@id, @name, @sex)";//设置带参SQL语句
- cmd.Parameters.AddRange(new[] {//添加参数
- new SQLiteParameter("@id", i),
- new SQLiteParameter("@name", "中国人"),
- new SQLiteParameter("@sex", "男")
- });
- cmd.ExecuteNonQuery();//执行查询
- }
- tran.Commit();//提交
- }
- }
- }
插入这样的十万条数据只需要5秒左右。
读取数据:
- static void Main(string[] args)
- {
- SQLiteConnection conn = null;
- string dbPath = "Data Source =" + Environment.CurrentDirectory + "/test.db";
- conn = new SQLiteConnection(dbPath);//创建数据库实例,指定文件位置
- conn.Open();//打开数据库,若文件不存在会自动创建
- string sql = "select * from student";
- SQLiteCommand cmdQ = new SQLiteCommand(sql, conn);
- SQLiteDataReader reader = cmdQ.ExecuteReader();
- while (reader.Read())
- {
- Console.WriteLine(reader.GetInt32(0) + " " + reader.GetString(1) + " " + reader.GetString(2));
- }
- conn.Close();
- Console.ReadKey();
- }
数据读取成功。
转自:C#使用System.Data.SQLite操作SQLite - 柚柚控 - ITeye技术网站
http://hzy3774.iteye.com/blog/1691932
0 0
- C#使用System.Data.SQLite操作SQLite
- C#使用System.Data.SQLite操作SQLite
- C#使用System.Data.SQLite操作SQLite
- C#使用System.Data.SQLite操作SQLite
- C#使用System.Data.SQLite操作SQLite
- C# system.data.sqlite使用
- C#引用System.Data.SQLite操作SQLite数据库一例
- VS2015 C# System.Data.SQLite
- C# System.Data.Sqlite.dll 的注意事项
- .Net4.0以上使用System.Data.Sqlite
- .Net4.0以上使用System.Data.Sqlite
- .Net4.0以上使用System.Data.Sqlite
- System.Data.SQLite
- System.Data.SQLite错误,
- C#2010 在使用 System.Data.SQLite.dll 时出现异常
- C# 调用System.Data.Sqlite 外键失效解决方案
- System.Data.SQLite数据库简介
- System.Data.SQLite数据库介绍
- Codeforces Round #371 (Div. 2) A.B.C
- C++类的访问控制
- 局域网访问PostgreSQL链接不成功解决方法
- 基于linux的3款压力测试工具:Siege,webbench,ab
- jQuery获取上传文件的名称
- C#使用System.Data.SQLite操作SQLite
- 二级指针输出模型
- 方法内的函数 ---之js this绑定的不合理
- Ajax学习心得
- Android IntentService 示例与详解
- Hadoop Streaming & Hive Tips
- sus 11.3如何安装与配置cloud-init
- 内存对齐.结构体对齐
- 关于ionic路由跳转中back button 不出现的问题