C#操作SQLite数据库

文章目录

1、C#操作SQLite数据库的帮助类

csharp 复制代码
public static class SQLiteHelper
{
    public static string ConStr = "";
    public static int Update(string sql)
    {
        SQLiteConnection DBConnection = new SQLiteConnection(ConStr);
        SQLiteCommand cmd = new SQLiteCommand(sql, DBConnection);
        try
        {
            DBConnection.Open();
            return cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            DBConnection.Close();
        }
    }

    public static object GetSingleResult(string sql)
    {
        SQLiteConnection DBConnection = new SQLiteConnection(ConStr);
        SQLiteCommand cmd = new SQLiteCommand(sql, DBConnection);
        try
        {
            DBConnection.Open();
            return cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {

            throw ex;
        }
        finally
        {
            DBConnection.Close();
        }
    }
    public static SQLiteDataReader GetReader(string sql)
    {
        SQLiteConnection DBConnection = new SQLiteConnection(ConStr);
        SQLiteCommand cmd = new SQLiteCommand(sql, DBConnection);
        try
        {
            DBConnection.Open();
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception ex)
        {
            DBConnection.Close();
            throw ex;
        }


    }
    public static DataSet GetDataSet(string sql)
    {
        SQLiteConnection DBConnection = new SQLiteConnection(ConStr);
        SQLiteCommand cmd = new SQLiteCommand(sql, DBConnection);
        SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
        DataSet ds = new DataSet();
        try
        {
            DBConnection.Open();
            da.Fill(ds);
            return ds;
        }
        catch (Exception)
        {
            return null;
        }
        finally
        {
            DBConnection.Close();
        }
    }
}

2、使用示例

2.1 项目中引用SQLite类库

在项目中引用System.Data.SQLite,或者直接去官网下载https://www.sqlite.org/index.html,完成之后,新建一个数据库文件,并加载到VS里面,如下图:

一般要用的SQL语句,都会在这里面测试好,再通过程序调用。

2.2 操作数据库示例

在其他类里面直接调用帮助类,也可以在窗体加载时调用

csharp 复制代码
private void Server_Load(object sender, EventArgs e)
{

  SQLiteHelper.ConStr= "Data Source=" + Application.StartupPath + "\\DataBase\\ScadaDataBase;Pooling=true;FailIfMissing=false"; 
  string sql1 = "insert into SysAdmins(LoginID, LoginName, LoginPwd, Role) values('10006', 'CFO', '008', '9')";
  int rtn =SQLiteHelper.Update(sql1);
}

运行一下,数据库里就插入了一条数据。

相关推荐
苜蓿muxu17 小时前
缓存三大问题:穿透、击穿、雪崩
数据库·redis
正在走向自律18 小时前
MySQL兼容性深度实践:建表、增删改查与迁移避坑全记录
数据库·sql·mysql·信创·数据库迁移
JonaHu18 小时前
关键词排名突然消失?用 Python、SQLite 和 SERP API 搭一套可解释的 SEO 监控
python·sqlite·seo·serp api
隔窗听雨眠19 小时前
openGauss开源五年的内核演进与生态工程
数据库·opengauss·国产数据库
Decay__19 小时前
PostgreSQL 体系架构全解 —— 进程模型、共享内存、MVCC 与 WAL 预写日志深入
运维·数据库·sql·mysql·架构
沐知全栈开发21 小时前
基于ClinicalTrials公开数据的临床试验分析系统-毕业设计
数据库·课程设计
半杯咖啡半行码21 小时前
Qt 隐式共享(Implicit Sharing)深度解析:机制、陷阱与 Qt6迁移
开发语言·数据库·qt
Nturmoils21 小时前
空值、日期、字符串:业务 SQL 里的函数先跑一遍
数据库
倔强的石头_1 天前
数据库选型四步法:我把候选从十几个缩到两个
数据库
bksczm2 天前
Redis Cluster 分片集群详解:槽路由、故障判定与扩缩容
数据库·redis·分布式