• SqliteHelper


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SQLite;
    using System.Configuration;
    
    namespace DAL
    {
        public class SqliteHelper
        {
            //获取连接字符串
            private static readonly string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    
            /// <summary>
            /// 做增删改的功能
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <param name="ps">SQL语句中的参数</param>
            /// <returns>受影响的行数</returns>
            public static int ExecuteNonQuery(string sql, params SQLiteParameter[] ps)
            {
                //连接数据库
                using (SQLiteConnection con=new SQLiteConnection(str))
                {
                    using (SQLiteCommand cmd =new SQLiteCommand(sql,con))
                    {
                        con.Open();//打开数据库
                        if (ps!=null)
                        {
                            cmd.Parameters.AddRange(ps);//参数,加集合(ps)
                        }
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
            
            /// <summary>
            /// 查询首行首列
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <param name="ps">SQL语句的参数</param>
            /// <returns>返回首行首列object</returns>
            public static object ExecuteScalar(string sql, params SQLiteParameter[] ps)
            {
                using (SQLiteConnection con=new SQLiteConnection(str))
                {
                    using (SQLiteCommand cmd=new SQLiteCommand(sql,con))
                    {
                        con.Open();
                        if (ps!=null)
                        {
                            cmd.Parameters.AddRange(ps);
                        }
                        return cmd.ExecuteScalar();
                    }
                }
            }
    
            /// <summary>
            /// 查询多行
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <param name="ps">SQL语句的参数</param>
            /// <returns>返回多行SQLiteDataReader</returns>
            public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] ps)
            {
                SQLiteConnection con = new SQLiteConnection(str);
                using (SQLiteCommand cmd=new SQLiteCommand(sql,con))
                {
                    if (ps!=null)
                    {
                        cmd.Parameters.AddRange(ps);
                    }
                    try
                    {
                        con.Open();
                        return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                    }
                    catch (Exception ex)
                    {
                        con.Close();
                        con.Dispose();
                        throw ex;
                    }
                }
            }
    
            /// <summary>
            /// 查询数据表
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <param name="ps">SQL语句中的参数</param>
            /// <returns>返回表DataTable</returns>
            public static DataTable ExecuteTable(string sql, params SQLiteParameter[] ps)
            {
                DataTable dt = new DataTable();
                using (SQLiteDataAdapter sda=new SQLiteDataAdapter(sql,str))
                {
                    if (ps!=null)
                    {
                        sda.SelectCommand.Parameters.AddRange(ps);
                    }
                    sda.Fill(dt);
                    return dt;
                }
            }
    
    
        }
    }
  • 相关阅读:
    Sql的同一机器不同数据库联合查询示例
    poi 公式自動重新計算
    js 取得文件大小
    java 读写文件
    Postman安装及入门实践(以百度搜索为例)
    PostmanTests模块测试方法记录
    HTML颜色代码
    网盘搜索器 v1.0
    线程知识点一:如何传入多个参数
    asp.net 实现省市级联<简单方法2>前台实现
  • 原文地址:https://www.cnblogs.com/blacop/p/6148531.html
Copyright © 2020-2023  润新知