• 单例模式


        class DBHelper
        {
            private static readonly string strConn = "Data Source=.;Integrated Security=true;Initial Catalog=myDB";
            private static SqlConnection sqlConn = null;
            private static DBHelper dbHelper = null;
    
            /// <summary>
            /// 数据库连接对象实例
            /// </summary>
            private DBHelper()
            {
                if (sqlConn == null)
                {
                    sqlConn = new SqlConnection(strConn);
                }
            }
    
            /// <summary>
            /// 取得/创建对象实例
            /// </summary>
            /// <returns></returns>
            internal static DBHelper GetIntance()
            {
                if (dbHelper == null)
                {
                    dbHelper = new DBHelper();
                }
                return dbHelper;
            }
    
            /// <summary>
            /// 打开数据库连接
            /// </summary>
            private void Open()
            {
                if (sqlConn.State == ConnectionState.Closed)
                {
                    sqlConn.Open();
                }
            }
    
            /// <summary>
            /// 关闭数据库连接
            /// </summary>
            private void Close()
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
    
            public int ExecuteSql(string strSql, SqlParameter[] para)
            {
                SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);
                if (para != null)
                {
                    sqlCmd.Parameters.AddRange(para);
                }
                Open();
                int objResult = sqlCmd.ExecuteNonQuery();
                Close();
                return objResult;
            }
    
            public DataSet GetSqlDataSet(string strSql, SqlParameter[] para)
            {
                SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);
                if (para != null)
                {
                    sqlCmd.Parameters.AddRange(para);
                }
                SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);
                DataSet ds = new DataSet();
                sqlAdp.Fill(ds);
                return ds;
            }
        }
            //调用实例
            private static void GetDBList()
            {
                DBHelper dbHelper = DBHelper.GetIntance();
                DataSet ds = dbHelper.GetSqlDataSet("SELECT TOP 20 [StuId],[StuName] FROM [Student]", null);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Console.WriteLine(ds.Tables[0].Rows[i][0].ToString() + ds.Tables[0].Rows[i][1].ToString());
                }
            }
  • 相关阅读:
    763. 划分字母区间(贪心算法)
    1282. 用户分组(贪心算法)
    698. 划分为k个相等的子集
    560. 和为K的子数组
    面试题 16.10. 生存人数
    Python:对列表进行排序并相应地更改另一个列表?
    数据竞赛总结
    面试提问之请你介绍一下xxx方法
    常用数学符号读法
    round() 函数
  • 原文地址:https://www.cnblogs.com/myjacky/p/3226798.html
Copyright © 2020-2023  润新知