zoukankan      html  css  js  c++  java
  • 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;
                }
            }
    
    
        }
    }
  • 相关阅读:
    smarty通过asssign传入的值不能在模板显示值
    如何选择合适的MySQL存储引擎
    列表 li 中的文字无法垂直居中
    php 中在 CCS文件中设置背景图片无效:
    完美实现个人建站梦想 全面了解IIS组建方法
    颂孔明
    <青春飞扬>诗集出版的历史价值
    市场部常用名词(培训资料整理)
    爱的脚步
    一位闯荡江湖多年的商人的感悟
  • 原文地址:https://www.cnblogs.com/blacop/p/6148531.html
Copyright © 2011-2022 走看看