首页 > 数据库 >C#操作sqlite数据库

C#操作sqlite数据库

时间:2024-07-30 11:22:15浏览次数:7  
标签:sqlite C# 数据库 cmd System connection using new Data

//连接字符串
conn = @"Data Source=E:\sqlite.db";
string sql_table = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '%prod%'";
DataTable dt_tbname = SQLiteHelper.Query(conn, sql_table).Tables[0];
//工具类SQLiteHelper

查看SQLiteHelper
using System;
using System.Collections;
using System.Data;
using System.Data.SQLite;
using System.Threading;
//using Topshelf.Logging;

public class SQLiteHelper
{
    //private static readonly LogWriter logger = HostLogger.Get<SQLiteHelper>();

    //public static string connectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\data\\data.db";

    public static string connectionString  = @"Data Source=E:\XXX.db";

    private static object ExecuteNonQuery_locker = new object();
    /// <summary>
            /// 执行SQL语句,返回影响的记录数
            /// </summary>
            /// <param name="SQLString">SQL语句</param>
            /// <returns>影响的记录数</returns>
    public static int ExecuteSql(string SQLString,out string sqlerr_msg)
    {
        sqlerr_msg = "";
        using (SQLiteConnection connection = new SQLiteConnection(connectionString))
        {
            using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
            {
                try
                {
                    connection.Open();
                    int row = cmd.ExecuteNonQuery();
                    return row;
                }
                catch (System.Data.SQLite.SQLiteException E)
                {
                    connection.Close();
                    sqlerr_msg = E.Message;
                    //logger.Error("插入數據異常Service-insert_path_sqlite  " + E.Message);
                    //throw new Exception(E.Message);
                }
            }
        }
        return 0;
    } 
    
    /// <summary>
            /// 执行SQL语句,返回影响的记录数
            /// </summary>
            /// <param name="SQLString">SQL语句</param>
            /// <returns>影响的记录数</returns>
    public static int ExecuteSql(string SQLString)
    {
        using (SQLiteConnection connection = new SQLiteConnection(connectionString))
        {
            using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
            {
                try
                {
                    connection.Open();
                    int row = cmd.ExecuteNonQuery();
                    return row;
                }
                catch (System.Data.SQLite.SQLiteException E)
                {
                    connection.Close();
                    throw new Exception(E.Message);
                }
            }
        }
        return 0;
    }

    /// <summary>
            /// 执行多条SQL语句,实现数据库事务。
            /// </summary>
            /// <param name="SQLStringList">多条SQL语句</param>    
    public static void ExecuteSqlTran(ArrayList SQLStringList)
    {
        using (SQLiteConnection conn = new SQLiteConnection(connectionString))
        {
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = conn;
            SQLiteTransaction tx = conn.BeginTransaction();
            cmd.Transaction = tx;
            try
            {
                for (int n = 0; n < SQLStringList.Count; n++)
                {
                    string strsql = SQLStringList[n].ToString();
                    if (strsql.Trim().Length > 1)
                    {
                        cmd.CommandText = strsql;
                        cmd.ExecuteNonQuery();
                    }
                }
                tx.Commit();
            }
            catch (System.Data.SQLite.SQLiteException E)
            {
                tx.Rollback();
                throw new Exception(E.Message);
            }
        }
    }


    /// <summary>
            /// 执行查询语句,返回DataSet
            /// </summary>
            /// <param name="SQLString">查询语句</param>
            /// <returns>DataSet</returns>
    public static DataSet Query(String conn, string SQLString)
    {
        using (SQLiteConnection connection = new SQLiteConnection(conn))
        {
            DataSet ds = new DataSet();
            try
            {
                connection.Open();
                SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
                command.Fill(ds, "ds");
            }
            catch (System.Data.SQLite.SQLiteException ex)
            {
                throw new Exception(ex.Message);
            }
            return ds;
        }
    }
}



标签:sqlite,C#,数据库,cmd,System,connection,using,new,Data
From: https://www.cnblogs.com/xynmw/p/18331943

相关文章

  • 在PC端打开微信接收的文件,会出现只读的情况,怎么办?
    如您在电脑端打开微信接收的文件,出现只读的情况,请先检查您的微信软件版本是否是3.9版本;如微信版本是3.9版本,该问题原因是由于微信升级,属于微信新特性。临时解决方案:1、建议另存为文件到其他的路径下再行打开。可以前往查看微信下载文件的目录,将文件夹目录权限中的【只读】取......
  • Python - Function Annotations
     deffunc(s:str,i:int,j:int)->str:returns[i:j]Theparametersissupposedtobeastring,soweplaceacolonaftertheparameternameandthenwritestr.Parametersiandjaresupposedtobeintegerssowewriteintforthem.Returntypeis......
  • 使用带有 pythonKit XCODE 的嵌入式 Python,在 iOS 应用程序中与 OpenCV-python 签名不
    我根据Beewares使用指南在XCODE中将Python嵌入到我的iOS项目中https://github.com/beeware/Python-Apple-support/blob/main/USAGE.md运行时,我得到pythonKit找不到由ultralytics导入的cv2错误。当我将OpenCV-python添加到我的app_packages文件夹时......
  • 计算 Pytorch 中数据标签的梯度
    我正在努力实现研究论文中的一项技术,我需要计算相对于数据标签的梯度。这是我正在遵循的方法:计算损失相对于模型参数(grad1)的梯度。计算grad1相对于数据标签的梯度。但是,我遇到一个问题,即相对于数据标签的梯度始终为“无”。似乎数据标签(y)不是计算......
  • 在Centos7中使用一键脚本安装Oracle11g
    在Centos7中使用一键脚本安装Oracle11g1.环境准备1.1系统版本:Centos7.9(2009)1.2Oracle版本:Oracle11g11.2.0.41.3网络需求:可以连接互联网1.4一键安装:curl-ooracle_install.shhttps://files-cdn.cnblogs.com/files/blogs/827077/oracle_install.sh?t=1722301473&&c......
  • CTFshow web入门vip 文件上传
    web151题目提示前端校验不可靠,看源码可以看到是传到upload.php这个文件去接受文件上传,文件类型为图片,后缀限制为png然后把前端验证修改一下,把文件后缀限制改成php写个一句话木马传进去1.php<?phpeval($_POST['x']);?>url中需要加入我们传入文件的目录/upload.php,并指定/......
  • ctfshow 每周大挑战 rce挑战1-5
    RCE挑战1RCE挑战2题目<?php//本题灵感来自研究Y4tacker佬在吃瓜杯投稿的shellme时想到的姿势,太棒啦~。error_reporting(0);highlight_file(__FILE__);if(isset($_POST['ctf_show'])){$ctfshow=$_POST['ctf_show'];if(is_string($ctfshow)){if(!......
  • Oracle内置SQL函数
    Oracle内置SQL函数F.1字符函数——返回字符值这些函数全都接收的是字符族类型的参数(CHR除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类型所受的限制是相同的,比如:VARCHAR2数值被限制为2000字符(O......
  • Spring 6 IoC
    Spring6IoC引言Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器,它帮助开发者创建对象并管理对象之间的依赖关系。IoC(InversionofControl,控制反转)是Spring框架的核心思想之一,它通过将对象的创建权和管理权交给Spring容器来降低代码的耦合度,提高程序的扩展性和可......
  • The Declaration of Independence
    TheDeclarationofIndependenceIntroductionHistoricalContextTheProclamationof1763TheSugarActandStampActTheTownshendActsTheBostonMassacreandTeaPartyTheIntolerableActsTheFirstContinentalCongressTheSecondContinentalCongressPreamb......