• C# 下sqlite简单使用


    1. 对数据库增, 删, 改

                //数据库文件存储路径,(Environment.CurrentDirectory:为当前工作目录的完全路径)
                string dbPath = "Data Source =" + Environment.CurrentDirectory + "/test.db";
                //创建连接数据库实例,指定文件位置  
                SQLiteConnection con = new SQLiteConnection(dbPath);
                //打开数据库,若文件不存在会自动创建  
                con.Open();
                //建表语句  
                string sql = "CREATE TABLE IF NOT EXISTS student(id integer, name varchar(20), sex varchar(2));";
                //创建sql执行指令对象
                SQLiteCommand com = new SQLiteCommand(sql, con);
                //如果不带参数时, 使用一下语句赋值
                //com.CommandText = sql;
                //com.Connection = con;
                //执行sql指令创建建数据表,如果表不存在,创建数据表  
                com.ExecuteNonQuery();
    
                //给表添加数据
                //1. 使用sql语句逐行添加
                com.CommandText = "INSERT INTO student VALUES(1, '小红', '男')";
                com.ExecuteNonQuery();
                com.CommandText = "INSERT INTO student VALUES(2, '小李', '女')";
                com.ExecuteNonQuery();
                com.CommandText = "INSERT INTO student VALUES(3, '小明', '男')";
                com.ExecuteNonQuery();
                //2. 使用事务添加
                //实例化一个事务对象
                SQLiteTransaction tran = con.BeginTransaction();
                //把事务对象赋值给com的transaction属性
                com.Transaction = tran;
                //设置带参数sql语句
                com.CommandText = "INSERT INTO student VALUES(@id, @name, @sex)";
                for (int i = 0; i < 10; i++)
                {
                    //添加参数
                    com.Parameters.AddRange(new[] {//添加参数  
                               new SQLiteParameter("@id", i + 1),  
                               new SQLiteParameter("@name", "test" + i),  
                               new SQLiteParameter("@sex", i % 3 == 0 ? "男" : "女")  
                           });
                    //执行添加
                    com.ExecuteNonQuery();
                }
                //提交事务
                tran.Commit();
                //关闭数据库
                con.Close();
    

      2. 读取数据

                //数据库路径
                string dbPath = "Data Source =" + Environment.CurrentDirectory + "/test.db";
                //创建数据库实例,指定文件位置  
                SQLiteConnection conn = new SQLiteConnection(dbPath);
                //打开数据库,若文件不存在会自动创建  
                conn.Open();
                //查询sql语句
                string sql = "select * from student";
                //实例化sql指令对象
                SQLiteCommand cmdQ = new SQLiteCommand(sql, conn);
                //存放读取数值
                SQLiteDataReader reader = cmdQ.ExecuteReader();
                //显示数据的控件
                richTextBox1.Text = "";
                //读取每一行数据
                while (reader.Read())
                {
                    //读取并赋值给控件
                    richTextBox1.Text += reader.GetInt32(0) + " " + reader.GetString(1) + " " + reader.GetString(2) + "
    ";
                }
                //关闭数据库
                conn.Close();
  • 相关阅读:
    unity HideInInspector与SerializeField
    Unity3D之游戏架构脚本该如何来写(转)
    写在创业公司工作后
    工作选择
    指针引用
    sqrt函数实现
    超平面
    【读书笔记】读《程序员面试宝典》
    【小白学游戏常用算法】一、随机迷宫算法
    【Cocos2d-x游戏开发】浅谈游戏中的坐标系
  • 原文地址:https://www.cnblogs.com/weloglog888/p/6366953.html
Copyright © 2020-2023  润新知