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

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

相关推荐
huohuopro6 分钟前
Hbase伪分布式远程访问配置
数据库·分布式·hbase
XDHCOM19 分钟前
ORA-12169: TNS连接标识符过长,Oracle报错故障修复与远程处理
数据库·oracle
爬山算法36 分钟前
MongoDB(86)如何使用MongoDB存储大文件?
数据库·mongodb
xcLeigh44 分钟前
KES数据库表空间目录自动创建特性详解与存储运维最佳实践
大数据·运维·服务器·数据库·表空间·存储
aini_lovee1 小时前
C# 快速搜索磁盘文件解决方案
开发语言·c#
小陈工1 小时前
2026年4月8日技术资讯洞察:边缘AI推理框架竞争白热化,Python后端开发者的机遇与挑战
开发语言·数据库·人工智能·python·微服务·回归
wb1891 小时前
NoSQL数据库Redis集群重习
数据库·redis·笔记·云计算·nosql
许杰小刀1 小时前
MyBatis-Plus实战:Spring Boot数据库操作效率提升10倍
数据库·spring boot·mybatis
Navicat中国1 小时前
Navicat 结构同步:一键解决多库结构不一致难题
数据库·navicat·结构同步
ggabb1 小时前
木卫二(欧罗巴)的潜在生命迹象与探测计划
sqlite