1.安装好sqllite

2.下载system.data.sqlite.dll

选择对应平台的框架版本

System.Data.SQLite: Downloads PageSystem.Data.SQLite: Downloads Page

3.C#操作sqlite

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

namespace connectSQLite
{
    public partial class Form1 : Form
    {
        string connectionString = "data source=" + @"D:\Soft-Execute\sqlite\demo.db";//必须demo.db 用绝对路径

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonExecute_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = Query("SELECT ID,Name FROM demoTable");
                this.dataGridView1.DataSource = ds.Tables[0];//一定要 ds.Tables[0]才会显示内容;= ds.Tables不显示!!!
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 执行查询语句,返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public DataSet Query(string SQLString)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SQLite.SQLiteException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }

    }
}

查询:

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐