C#Sqlite数据库操作介绍以及类库介绍

有段时间未更新过博客了,觉得有用的对象,有时间的话会继续更新

1. VS2022中,用Nug包管理器,引入System.Data.sqlite包

转载请标明出处:Python Excellent的博客
在这里插入图片描述

2. 新建一个Sqlite帮助类SqliteHelper.cs

具体方法如下:基本可以应对增删改查,以及事务处理数据库,保证当数据库操作数据失败,具有回滚操作,而不影响数据库的查询。

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data.Common;
using System.Collections;

namespace  ZYL9527
{
   
    class SqliteHelper : IDisposable
    {
   
        public SQLiteConnection conn;

        public void Dispose()
        {
   
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
   
            if (disposing)
                if (conn != null)
                {
   
                    conn.Dispose();
                    conn = null;
                }
        }

        ~SqliteHelper()
        {
   
            Dispose(false);
        }

        /// <summary>
        /// 构造函数。
        /// </summary>
        /// <param name="dataBaseName">数据库名</param>
        public SqliteHelper(string dataBaseName)
        {
   
            string connString = string.Format(@"Data Source={0}", dataBaseName);
            conn = new SQLiteConnection(connString);
            //conn.Open();
        }

        /// <summary>
        /// 手动打开数据库。
        /// </summary>
        public bool SqliteOpen()
        {
   
            if (conn == null || conn.State != ConnectionState.Closed)
                return false;
            try
            {
   

                conn.Open();
                return true;
            }
            catch (Exception ex)
            {
   
                return false;
            }
        }

        /// <summary>
        /// 通过执行SQL语句,获取表中数据。
        /// </summary>
        /// <param name="sError">错误信息</param>
        /// <param name="sSQL">执行的SQL语句</param>
        public DataTable GetDataTable(out string sError, string sSQL)
        {
   
            DataTable dt = null;
            sError = string.Empty;
            try
            {
   
                SQLiteCommand cmd = new SQLiteCommand() {
    CommandText = sSQL, Connection = conn };
                SQLiteDataAdapter dao = new SQLiteDataAdapter(cmd);
                dt = new DataTable();

                dao.Fill(dt);
                //SQLiteDataReader dr = cmd.ExecuteReader();
                //if (dr.HasRows)
                //  dt.Load(dr);
            }
            catch (Exception e)
            {
   
                sError = e.Message;
            }
            return dt;
        }

        /// <summary>
        /// 通过执行SQL语句,获取表中数据个数。
        /// 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python Excellent

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值