C#使用SQLite数据库

VS使用SQLite数据库(主要参考增删改查的代码与新建数据库文件)
参考链接:https://my.oschina.net/u/4518054/blog/4755744?hmsr=kaifa_aladdin
引用——》添加——》管理NuGet程序包——》System.Data.SQLite

SQLite的安装及使用(主要参考新建数据库文件)
参考链接:https://blog.csdn.net/weixin_41656968/article/details/80338626
下载地址:https://www.sqlite.org/download.html
只是创建SQLite数据库下载sqlite-tools-win32-x86-3370200.zip即可

增删改查封装类:https://www.cnblogs.com/123fang/articles/11433033.html
封装类实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.SQLite;
namespace SQLiteTest
{
    class sqliteHelper
    {
        static readonly string DB_PATH = "Data Source=D:/software Installation/sqlite-tools-win32-x86-3370200/test.db";
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="SQL"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static DataTable SelectDB(string SQL, string db = "MydataBase")
        {
            DataTable dt = new DataTable();
            try
            {
                Console.WriteLine("查询数据,SQL:" + SQL);
                string dbFile = System.AppDomain.CurrentDomain.BaseDirectory + db + @".db";
                string connString = string.Format("Data Source={0};Pooling=true;FailIfMissing=false", dbFile);//拼全连接字符串
                SQLiteConnection conn = new SQLiteConnection(DB_PATH);//当前连接字符串写死了
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(SQL, conn);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                dt = ds.Tables[0];
                //关闭连接,释放资源
                conn.Close();
                conn.Dispose();
                adapter.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine("查询异常:" + ex.ToString());
                return dt;
            }
            return dt;
        }

        #region 增删改数据
        /// <summary>
        /// 增删改数据
        /// </summary>
        /// <param name="SQL"></param>
        /// <param name="db">操作的数据库名称</param>
        /// <returns></returns>
        public static bool OperationDB(string SQL, string db = "MydataBase")
        {

            try
            {
                Console.WriteLine("增删改数据,SQL:" + SQL);
                string dbFile = System.AppDomain.CurrentDomain.BaseDirectory + db + @".db";
                string connString = string.Format("Data Source={0};Pooling=true;FailIfMissing=false", dbFile);
                SQLiteConnection conn = new SQLiteConnection(DB_PATH);
                conn.Open();
                string Sql = string.Format(SQL);
                SQLiteCommand insertData = new SQLiteCommand(Sql, conn);
                insertData.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("增删改数据,异常:" + ex.ToString());
                return false;
            }
            return true;
        }

        #endregion
    }
}

封装类的使用:

            string sql = " SELECT * FROM Test_User ";
            DataTable dt = sqliteHelper.SelectDB(sql);
            for(int i = 0 ; i < dt.Rows.Count ; i++)     
            {
                Console.WriteLine("name=" + dt.Rows[i]["name"].ToString() + "   age=" + dt.Rows[i]["age"].ToString());   
            }     
            string insertSql = "INSERT INTO Test_User values('444','444')";
            var dt2= sqliteHelper.OperationDB(insertSql);
            Console.WriteLine( "dt2=" + dt2);
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值