• C# 简单的SQLHelper


     public class SQLHelper
        {
            private string m_SqlConnectionString = "";
            private SqlConnection conn = null;
    
            public SQLHelper(string sqlConnectionString)
            {
                m_SqlConnectionString = sqlConnectionString;
                if (!TestSqlConnection()) throw new Exception("数据库连接失败");
            }
    
            private SqlConnection GetConn()
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                return conn;
            }
            private bool TestSqlConnection()
            {
                bool IsCanConnect = true;
                try
                {
                    conn = new SqlConnection(m_SqlConnectionString);
                    conn.Open();
                }
                catch
                {
                    IsCanConnect = false;
                }
                finally
                {
                    conn.Close();
                }
                return IsCanConnect;
            }
    
            /// <summary>
            /// 执行增删改操作
            /// </summary>
            /// <param name="cmdText"></param>
            /// <param name="cmdType"></param>
            /// <returns></returns>
            public int ExecuteNoQuery(string cmdText, CommandType cmdType)
            {
                int ret = 0;
                try
                {
                    using (SqlCommand cmd = new SqlCommand(cmdText, GetConn()))
                    {
                        cmd.CommandType = cmdType;
                        ret = cmd.ExecuteNonQuery();
                    }
                    return ret;
                }
                catch
                {
                    conn.Close();
                    throw;
                }
            }
            /// <summary>
            /// 执行查询操作
            /// </summary>
            /// <param name="cmdText"></param>
            /// <param name="cmdType"></param>
            /// <returns></returns>
            public DataTable ExecuteQuery2DataTable(string cmdText, CommandType cmdType)
            {
                DataTable dt = null;
                try
                {
                    using (SqlCommand cmd = new SqlCommand(cmdText, GetConn()))
                    {
                        cmd.CommandType = cmdType;
                        using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            dt = new DataTable();
                            dt.Load(sdr);
                        }
                    }
                    return dt;
                }
                catch
                {
                    conn.Close();
                    throw;
                }
            }
            /// <summary>
            /// 执行查询操作
            /// </summary>
            /// <param name="cmdText"></param>
            /// <param name="cmdType"></param>
            /// <returns></returns>
            public DataSet ExecuteQuery2DataSet(string cmdText)
            {
                DataSet ds = null;
                try
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmdText, GetConn()))
                    {
                        ds = new DataSet();
                        da.Fill(ds);
                    }
                    return ds;
                }
                catch
                {
                    conn.Close();
                    throw;
                }
            }
        }

    作者:Mr.Jimmy
    出处:https://www.cnblogs.com/JHelius
    联系:yanyangzhihuo@foxmail.com
    如有疑问欢迎讨论,转载请注明出处

  • 相关阅读:
    NVCC编译器
    BMP文件格式及读写
    Vim 命令 【转】
    NFS文件系统配置 和 GLIBC更新
    cuda vector addition
    hdu4758 Walk Through Squares 自动机+DP
    hdu4722 Good Numbers
    hdu4725 The Shortest Path in Nya Graph
    Anaconda-科学计算的 Python 发行版
    OUC_NewACMer_Personal_#1 题解
  • 原文地址:https://www.cnblogs.com/JHelius/p/14318914.html
Copyright © 2020-2023  润新知