【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;
        }
    }
}
复制代码




本文转自叶超Luka博客园博客,原文链接:http://www.cnblogs.com/yc-755909659/p/3642945.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值