程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> SQLHelper,sqlhelper類

SQLHelper,sqlhelper類

編輯:C#入門知識

SQLHelper,sqlhelper類


     /// <summary>
        /// 獲取連接字符串
        /// </summary>
        private static readonly string constr = ConfigurationManager.ConnectionStrings["dbUser"].ConnectionString;

        /// <summary>
        /// 返回受影響行數(非查詢語句)
        /// </summary>
        /// <param name="sql">SQL語句</param>
        /// <param name="parameters">參數化查詢</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 返回查詢結果集中的第一行第一列
        /// </summary>
        /// <param name="sql">SQL語句</param>
        /// <param name="parameters">參數化查詢</param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();
                }
            }
        }

        /// <summary>
        /// 返回查詢結果有多條數據,數據放在數據庫中
        /// </summary>
        /// <param name="sql">SQL語句</param>
        /// <param name="parameters">參數化查詢</param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteReader();
                }
            }
        }

        /// <summary>
        /// 返回查詢結果有多條數據,數據放在本地緩存中
        /// </summary>
        /// <param name="sql">SQL語句</param>
        /// <param name="parameters">參數化查詢</param>
        /// <returns></returns>
        public static DataTable ExecuteDataSet(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
          conn.Open() using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); DataSet dataset = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dataset); return dataset.Tables[0]; } } } /// <summary> /// 將數據庫中的Null轉化為null /// </summary> /// <param name="value"></param> /// <returns></returns> public static object OutputNull(object value) { if (value == DBNull.Value) return null; else return value; } /// <summary> /// 將輸入數據的null轉化為數據庫中Null /// </summary> /// <param name="value"></param> /// <returns></returns> public static object InputNull(object value) { if (value == null) return DBNull.Value; else return value; } }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved