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();  
        }  
当然这种方法插入数据效率不高,数据量大的话要使用下面这种方法:
 

```csharp
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();  
        }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Seven Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值