首页 > 代码库 > C#使用System.Data.SQLite操作SQLite
C#使用System.Data.SQLite操作SQLite
使用System.Data.SQLite
下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
得到System.Data.SQLite.dll添加到工程引用;
建表,插入操作
C#代码
- 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/
建表成功。
当然这种方法插入数据效率不高,数据量大的话要使用下面这种方法:
C#代码
- 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秒左右。
读取数据:
C#代码
- 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();
- }
数据读取成功。
<iframe id="google_ads_frame5" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402380613&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3764819.html&dt=1402380616202&shv=r20140603&cbv=r20140417&saldr=sb&correlator=1402380615563&frm=20&ga_vid=469728998.1401679695&ga_sid=1402377708&ga_hid=1455283777&ga_fc=1&u_tz=480&u_his=71&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=0&ady=53105&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=5&xpc=f2NZXbyOw0&p=http%3A//www.cnblogs.com&dtd=50" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame5" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame6" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402380613&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3764819.html&dt=1402380616273&shv=r20140603&cbv=r20140417&saldr=sb&prev_slotnames=8660799060&correlator=1402380615563&frm=20&ga_vid=469728998.1401679695&ga_sid=1402377708&ga_hid=1455283777&ga_fc=1&u_tz=480&u_his=71&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=304&ady=54458&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=6&xpc=N0sBNE001O&p=http%3A//www.cnblogs.com&dtd=42" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame6" marginWidth="0" scrolling="no" hspace="0"></iframe>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。