一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

使用DBHelper

 goodwangLib 2014-10-13

處女作代碼 o(∩_∩)o...哈哈!

 

使用DBHelper完成數(shù)據(jù)層的訪問 及 執(zhí)行

 

    public class DBHelper
    {
        private static string connString;
        static DBHelper()
        {
            connString = ConfigurationManager.ConnectionStrings["MySchoolConnectionString"].ToString();
        }

        public SqlConnection GetConnection(bool hasOpen)
        {
            SqlConnection conn = new SqlConnection(connString);
            if (hasOpen)
                conn.Open();
            return conn;
        }


        public DataSet GetDataSet(string sql, CommandType cmdType, string dsName)
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(connString);
            SqlDataAdapter objAdapter = new SqlDataAdapter(sql, conn);
            objAdapter.SelectCommand.CommandType = cmdType;
            objAdapter.Fill(ds, dsName);
            conn.Close();
            conn.Dispose();
            return ds;
        }

        public SqlDataReader GetReader(string sql, CommandType cmdType, params SqlParameter[] sqlParams)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = cmdType;
            cmd.CommandText = sql;
            if (sqlParams != null)
                cmd.Parameters.AddRange(sqlParams);
            SqlConnection conn = GetConnection(true);
            cmd.Connection = conn;
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

 

        public int ExecuteSql(string sql, CommandType cmdType, params SqlParameter[] sqlParams)
        {
            int ret = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = cmdType;
            cmd.CommandText = sql;
            if (sqlParams != null)
                cmd.Parameters.AddRange(sqlParams);
            SqlConnection conn = GetConnection(true);
            cmd.Connection = conn;
            try
            {
                ret = (int)cmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }
}
 

 

調(diào)用DBHelper:

 

    public class GradeService
    {
        #region Private Members
        //從配置文件中讀取數(shù)據(jù)庫(kù)連接字符串
        private readonly string connString = ConfigurationManager.ConnectionStrings["MySchoolConnectionString"].ToString();
        private readonly string dboOwner = ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();
        #endregion

        #region Public Methods
        /// <summary>
        /// 獲得全部年級(jí)信息
        /// </summary>
        /// <returns>年級(jí)數(shù)據(jù)集</returns>
        public DataSet GetAllGrades()
        {
            DBHelper dbHelper = new DBHelper();
            return dbHelper.GetDataSet(dboOwner+".usp_SelectGradesAll",CommandType.StoredProcedure,"grade");
        }
        /// <summary>
        /// 根據(jù)年級(jí)名稱獲得年級(jí)ID
        /// </summary>
        /// <param name="gradeName">年級(jí)名稱</param>
        /// <returns>年級(jí)ID</returns>
        public int GetGradeIDByGradeName(string gradeName)
        {
            int number = 0;
            DBHelper dbHelper = new DBHelper();
            SqlParameter sqlParams = new SqlParameter("@GradeName", SqlDbType.NVarChar, 50);
            sqlParams.Value = gradeName;
            SqlDataReader objReader =dbHelper.GetReader(dboOwner + ".usp_SelectGradeByGradeName", CommandType.StoredProcedure, sqlParams);
            if (objReader.Read())
                number = Convert.ToInt32(objReader["GradeID"]);
            objReader.Close();
            objReader.Dispose();
            return number;
        }
        /// <summary>
        /// 新增年級(jí)
        /// </summary>
        public void AddGrade(DataSet dsGrade)
        {
            DBHelper dbHelper = new DBHelper();
            SqlParameter[] sqlParams= new SqlParameter[1];
            sqlParams[0] = new SqlParameter("@GradeName", SqlDbType.NVarChar, 50);
            //sqlParams[1] = new SqlParameter("@GradeID", SqlDbType.Int);
            sqlParams[0].Value = dsGrade.Tables["Grade"].Rows[0]["GradeName"];
            //sqlParams[1].Value = 0;
            dbHelper.ExecuteSql(dboOwner + ".usp_SelectGradeByGradeName", CommandType.StoredProcedure, sqlParams);
        }
        #endregion
    }

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    熟女体下毛荫荫黑森林自拍| 色哟哟国产精品免费视频| 精品少妇一区二区视频| 国产免费成人激情视频| 高中女厕偷拍一区二区三区| 99在线视频精品免费播放| 91欧美一区二区三区成人| 欧美欧美欧美欧美一区| 欧美日韩免费观看视频| 日韩美成人免费在线视频| 日韩精品一级片免费看| 国产精品一区二区丝袜| 国产一级片内射视频免费播放| 亚洲黄香蕉视频免费看| 在线免费国产一区二区| 欧美日韩一级aa大片| 在线中文字幕亚洲欧美一区 | 五月激情婷婷丁香六月网| 免费观看潮喷到高潮大叫| 欧美人与动牲交a精品| 福利专区 久久精品午夜| 亚洲免费视频中文字幕在线观看| 国产香蕉国产精品偷在线观看| 99久久精品午夜一区| 日本一本在线免费福利| 美国欧洲日本韩国二本道| 久久青青草原中文字幕| 欧美综合色婷婷欧美激情| 嫩草国产福利视频一区二区| 欧美乱妇日本乱码特黄大片| 日韩中文无线码在线视频| 欧美日韩国产综合特黄| 狠狠做深爱婷婷久久综合| 国产av一二三区在线观看| 在线中文字幕亚洲欧美一区| 欧美成人黄色一区二区三区| 无套内射美女视频免费在线观看 | 中文字幕人妻一区二区免费| 欧美成人国产精品高清| 日韩一区二区三区嘿嘿| 亚洲伦理中文字幕在线观看|