C#连接sqlite
1.安装好sqllite2.下载system.data.sqlite.dllSystem.Data.SQLite: Downloads Page3.
·
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;
}
}
}
}
查询:
更多推荐
已为社区贡献1条内容
所有评论(0)