zoukankan      html  css  js  c++  java
  • 【C#】SQL数据库助手类1.0(自用)

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace Erp.DBUtility
    {
        public class YSqlHelper
        {
            //Sql连接语句
            private static string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
    
            /// <summary>执行不带参数的增删改SQL语句或存储过程 ,返回受影响的行数</summary>
            public static int ExecuteNonQuery(string cmdText, CommandType ct)
            {
                int res = 0;//受影响的行数
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    try
                    {
                        conn.Open();//打开数据库链接
                        using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                        {
                            cmd.CommandType = ct;
                            res = cmd.ExecuteNonQuery();//执行Sql语句并受影响的行数
                        }
                    }
                    catch
                    {
    
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
                        {
                            conn.Close();//关闭与数据库的链接
                        }
                    }
                }
                return res;
            }
    
            /// <summary>  执行带参数的增删改SQL语句或存储过程,返回受影响的行数</summary>
            public static int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)
            {
                int res = 0;//受影响的行数
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    try
                    {
                        conn.Open();//打开数据库链接
                        using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                        {
                            cmd.CommandType = ct;
                            cmd.Parameters.AddRange(paras);
                            res = cmd.ExecuteNonQuery();//执行Sql语句并受影响的行数
                        }
                    }
                    catch
                    {
    
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
                        {
                            conn.Close();//关闭与数据库的链接
                        }
                    }
                }
                return res;
            }
    
            /// <summary> 执行不带参数的查询SQL语句或存储过程,返回DataTable对象</summary>
            public static DataTable ExecuteQueryDataTable(string cmdText, CommandType ct)
            {
                DataTable dt = new DataTable();
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    try
                    {
                        conn.Open();//打开数据库链接
                        using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                        {
                            cmd.CommandType = ct;
                            using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                            {
                                dt.Load(sdr);
                            }
                        }
                    }
                    catch
                    {
    
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
                        {
                            conn.Close();//关闭与数据库的链接
                        }
                    }
                }
                return dt;
            }
    
            /// <summary> 执行带参数的查询SQL语句或存储过程,返回DataTable对象</summary>
            public static DataTable ExecuteQueryDataTable(string cmdText, SqlParameter[] paras, CommandType ct)
            {
                DataTable dt = new DataTable();
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    try
                    {
                        conn.Open();//打开数据库链接
                        using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                        {
                            cmd.CommandType = ct;
                            cmd.Parameters.AddRange(paras);
                            using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                            {
                                dt.Load(sdr);
                            }
                        }
                    }
                    catch
                    {
    
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
                        {
                            conn.Close();//关闭与数据库的链接
                        }
                    }
                }
                return dt;
            }
    
            /// <summary> 执行不带参数的查询SQL语句或存储过程,返回DataSet对象</summary>
            public static DataSet ExecuteQueryDataSet(string cmdText, CommandType ct)
            {
                DataSet ds = new DataSet();
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    try
                    {
                        conn.Open();//打开数据库链接
                        using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                        {
                            cmd.CommandType = ct;
                            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                            {
                                da.Fill(ds, "ds");
                            }
                        }
                    }
                    catch
                    {
    
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
                        {
                            conn.Close();//关闭与数据库的链接
                        }
                    }
                }
                return ds;
            }
    
            /// <summary> 执行带参数的查询SQL语句或存储过程,返回DataSet对象</summary>
            public static DataSet ExecuteQueryDataSet(string cmdText, SqlParameter[] paras, CommandType ct)
            {
                DataSet ds = new DataSet();
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    try
                    {
                        conn.Open();//打开数据库链接
                        using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                        {
                            cmd.CommandType = ct;
                            cmd.Parameters.AddRange(paras);
                            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                            {
                                da.Fill(ds, "ds");
                            }
                        }
                    }
                    catch
                    {
    
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
                        {
                            conn.Close();//关闭与数据库的链接
                        }
                    }
                }
                return ds;
            }
    
            /// <summary>查询数据是否存在</summary>
            public static bool ExecuteDataIsExistByData(string sqlStr, CommandType commandType)
            {
                bool iss = false;
                DataSet ds = ExecuteQueryDataSet(sqlStr, commandType);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    if (ds.Tables[i].Rows.Count > 0) iss = true;
                }
                return iss;
            }
    
            /// <summary>查询数据是否存在 </summary>
            public static bool ExecuteDataIsExistByData(string sqlStr, SqlParameter[] paras, CommandType commandType)
            {
                bool iss = false;
                DataSet ds = ExecuteQueryDataSet(sqlStr, paras, commandType);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    if (ds.Tables[i].Rows.Count > 0) iss = true;
                }
                return iss;
            }
            /// <summary>查询增删改数据操作是否成功 </summary>
            public static bool ExecuteDataIsExistByInt(string sqlStr, CommandType commandType)
            {
                int ds = ExecuteNonQuery(sqlStr, commandType);
                bool iss = ds > 0 ? true : false;
                return iss;
            }
            /// <summary>查询增删改数据操作是否成功 </summary>
            public static bool ExecuteDataIsExistByInt(string sqlStr, SqlParameter[] paras, CommandType commandType)
            {
                int ds = ExecuteNonQuery(sqlStr, paras, commandType);
                bool iss = ds > 0 ? true : false;
                return iss;
            }
        }
    }
  • 相关阅读:
    轮循与连接
    ps中锯齿问题的解决方法
    Linux+Apache+MySQL+PHP安装
    Map map=new HashMap()与HashMap map=new HashMap()的区别
    wdcp常用工具及命令集
    ecshop调试
    纯净版CentOS64位安装LAMP的时候出现的问题总结
    Alertmanager配置webhook
    一个Repeater排序用的控件
    一道面试题模拟实现简易的移动用户资费统计系统逻辑
  • 原文地址:https://www.cnblogs.com/yc-755909659/p/3642945.html
Copyright © 2011-2022 走看看