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

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

相关推荐
hans汉斯2 小时前
嵌入式操作系统技术发展趋势
大数据·数据库·物联网·rust·云计算·嵌入式实时数据库·汉斯出版社
Coder_Boy_2 小时前
Spring 核心思想与企业级最佳特性(实践级)事务相关
java·数据库·spring
+VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue宠物寄养系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计·宠物
烛阴3 小时前
C# 正则表达式(3):分组与捕获——从子串提取到命名分组
前端·正则表达式·c#
一 乐3 小时前
校园实验室|基于springboot + vue校园实验室管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
坚定信念,勇往无前3 小时前
mongodb备份的脚本
数据库·mongodb
杨忆4 小时前
构建自己的开发工作台MFC
数据库·c++·mfc
阿巴~阿巴~4 小时前
告别命令行:Navicat可视化操作数据库(附安装使用教程)
服务器·数据库·mysql·navicat·可视化操作数据库
zhangyifang_0094 小时前
MySQL中实现“小计”与“合计”
数据库·mysql