DB类

WBOY
發布: 2016-06-07 15:48:53
原創
1725 人瀏覽過

namespace PlasticDAL { public class DBHelper { //定义数据库链接属性 private static SqlConnection objSqlConnection; /// summary /// 获得数据库连接状态 /// /summary public static SqlConnection ObjSqlConnection { get { string DBConnectionStri

namespace PlasticDAL
{
  
    public class DBHelper
    {

        //定义数据库链接属性
        private static SqlConnection objSqlConnection;

        ///


        /// 获得数据库连接状态
        ///

        public static SqlConnection ObjSqlConnection
        {
            get
            {
                string DBConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
                if (objSqlConnection == null)
                {
                    objSqlConnection = new SqlConnection(DBConnectionString);
                    objSqlConnection.Open();
                }
                else if (objSqlConnection.State == System.Data.ConnectionState.Closed)
                {
                    objSqlConnection.Open();
                }
                else if (objSqlConnection.State == System.Data.ConnectionState.Broken)
                {
                    objSqlConnection.Close();
                    objSqlConnection.Open();
                }
                return objSqlConnection;
            }
        }

        public static void CloseConn()
        {
            if (objSqlConnection.State==ConnectionState.Open)
            {
                objSqlConnection.Close();
            }
        }
         public static int ExecuteNonQueryBySql(string sql)
        {
            SqlCommand objSqlCommand = new SqlCommand(sql, ObjSqlConnection);
          
            return objSqlCommand.ExecuteNonQuery();
        }

         public static SqlDataReader GetReaderBySql(string sql)
        {

            SqlCommand cmd = new SqlCommand(sql, ObjSqlConnection);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }

             
 
    }
}

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板