首页 > 代码库 > 三层架构学习

三层架构学习

技术分享

技术分享

注意:技术分享

 

 

技术分享

技术分享

技术分享

 public class DBHelper
    {
        private static SqlConnection conn = new SqlConnection("server=.;database=HR;uid=sa;pwd=;");
        /// <summary>
        /// 封装SqlCommand对象的ExecuteNonQuery方法
        /// </summary>
        /// <param name="strsql">传入以insert|update|delete打头的SQL语句</param>
        /// <returns>返回执行影响的行数</returns>
        public static int ExecuteNonQuery(string strsql)
        {
            SqlCommand comm = new SqlCommand(strsql, conn);
            try
            {
                conn.Open();
                return comm.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 返回数据集DataSet
        /// </summary>
        /// <param name="strsql">以Select打头的SQL语句</param>
        /// <returns>返回DataSet对象</returns>
        public static DataSet GetDataSet(string strsql)
        {
            SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
                return ds;
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 返回DataTable对象
        /// </summary>
        /// <param name="strsql">以Select打头的SQL语句</param>
        /// <returns>返回DataTable对象</returns>
        public static DataTable GetDataTable(string strsql)
        {
            DataSet ds = GetDataSet(strsql);
            if (ds != null)
            {
                return ds.Tables[0];
            }
            else
                return null;
        }
        /// <summary>
        /// 返回首行首列的单元格的值(封装Command对象的ExecuteScalar方法)
        /// </summary>
        /// <param name="strsql">Select打头的SQL语句</param>
        /// <returns>返回的是Object</returns>
        public static object GetScalar(string strsql)
        {
            SqlCommand comm = new SqlCommand(strsql, conn);
            try
            {
                conn.Open();
                return comm.ExecuteScalar();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
        }
    }

注意:此时没有对sql注入考虑

 

三层架构学习