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);
}

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

相关推荐
AIgorithmGEEK2 小时前
MySQL 索引全解(下):建、查、删实战与知识速查表
数据库·mysql·b+树·索引
唠点键盘之外的4 小时前
26 向量数据库实战:从 0 搭一条能用的检索链路(pgvector + Spring AI)
数据库·人工智能·spring
云和恩墨4 小时前
数据库监控简史:监控不止于告警——从图表大爆发到健康管理闭环
数据库
ly76895 小时前
MongoDB 事务开销实测:快照隔离下的写冲突、重试与分片事务边界
数据库·mongodb·事务·分片集群·快照隔离·写冲突
豆角焖肉5 小时前
Spring AOP 自定义注解实现系统操作日志
数据库·自定义注解·springaop·操作日志·aop 切面
金伟API10246 小时前
不会写复杂SQL怎么办?
数据库·sql
执笔为剑6 小时前
2.1.2-1、V8R3迁移到V8R6-Oracle兼容特性
数据库
geovindu7 小时前
sql:Search-Optimized SQL Queries using mysql 9.0
android·数据库·sql·mysql·数据库开发·数据库架构
for_ever_love__7 小时前
MySQL SELECT 与 WHERE 优化实战:可搜索条件、联合索引与执行计划
数据库·mysql·oracle·select·where·条件优化·查询优化技巧
2501_931803757 小时前
MySQL 锁机制详解
数据库·mysql