C#使用SQLite

SQLite介绍

SQLite读[sk'laɪt]

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

SQLite是一个开源、免费的小型RDBMS(关系型数据库),能独立运行、无服务器、零配置、支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准。

SQLite数据库官方主页:http://www.sqlite.org/index.html

SQLite的SQL语法

C#操作SQLite Database

C#下SQLite操作驱动dll下载:System.Data.SQLite

C#使用SQLite步骤:

(1)新建一个project

(2)添加SQLite操作驱动System.Data.SQLite.dll引用

(3)使用API操作SQLite DataBase

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.IO;

namespace SQLiteDemo1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string datasource = Application.StartupPath + @"\test.db";

            bool isExist = File.Exists(datasource);
            if (!isExist)
            {
                //创建数据库文件
                System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
            }
            //连接数据库
            System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();
            
            System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
            connstr.DataSource = datasource;
            connstr.Password = "admin";//设置密码,SQLite ADO.NET实现了数据库密码保护
            conn.ConnectionString = connstr.ToString();
            conn.Open();

            System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
            cmd.Connection = conn;
            string sql = "";
            if (!isExist)
            {
                //创建数据库文件
                sql = "CREATE TABLE test(username varchar(20),password varchar(20))";
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }

            //插入数据
            sql = "INSERT INTO test VALUES('name_ludongbao','pwd_ludongbao')";
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            //取出数据
            sql = "SELECT * FROM test";
            cmd.CommandText = sql;
            System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();
            StringBuilder sb = new StringBuilder();
            while (reader.Read())
            {
                sb.Append("username:").Append(reader.GetString(0)).Append("\n")
                .Append("password:").Append(reader.GetString(1)).Append("\n");
            }
            MessageBox.Show(sb.ToString());
        }
    }
}

关于SQLite的connection string说明:http://www.connectionstrings.com/sqlite/

SQLite GUI客户端列表:http://www.sqlite.org/cvstrac/wiki?p=ManagementTools

SQLite Administrator下载地址:http://download.orbmu2k.de/files/sqliteadmin.zip

posted @ 2016-08-25 16:17  kylin2016  阅读(406)  评论(0编辑  收藏  举报