1. 程式人生 > >SQL-sqlHelper001

SQL-sqlHelper001

namespace decrypt ret n) ack ase 返回 使用 cmd

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace StudentManager.Dao
{
    /// <summary>
    ///  通用數據訪問類
    /// </summary>
    class SQLHelper
    {
        private static
readonly string connString = "Server=.;DataBase=StudentManager;Uid=sa;Pwd=password01!"; // public static readonly string connString = ConfigurationManager.ConnectionStrings["connString"].ToString(); //private static readonly string connString = // Common.StringSecurity.DESDecrypt(ConfigurationManager.ConnectionStrings["connString"].ToString());
#region 執行格式化的SQL語句 /// <summary> /// 執行增、刪、改(insert/update/delete) /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int Update(string sql) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd
= new SqlCommand(sql, conn); try { conn.Open(); int result = cmd.ExecuteNonQuery(); return result; } catch (Exception ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 執行單一結果查詢(select) /// </summary> /// <param name="sql"></param> /// <returns></returns> public static object GetSingleResult(string sql) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); try { conn.Open(); object result = cmd.ExecuteScalar(); return result; } catch (Exception ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 執行多結果查詢(select) /// </summary> /// <param name="sql"></param> /// <returns></returns> public static SqlDataReader GetReader(string sql) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); try { conn.Open(); SqlDataReader objReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); return objReader; } catch (Exception ex) { conn.Close(); throw ex; } } /// <summary> /// 執行返回數據集的查詢 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataSet GetDataSet(string sql) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter da = new SqlDataAdapter(cmd); //創建數據適配器對象 DataSet ds = new DataSet();//創建一個內存數據集 try { conn.Open(); da.Fill(ds); //使用數據適配器填充數據集 return ds; //返回數據集 } catch (Exception ex) { throw ex; } finally { conn.Close(); } } #endregion #region 帶參數的SQL語句 public static int Update(string sql, SqlParameter[] parameter) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); try { conn.Open(); cmd.Parameters.AddRange(parameter);//為Command對象添加參數 //foreach (SqlParameter item in parameter) //{ // cmd.Parameters.Add(item); //} int result = cmd.ExecuteNonQuery(); return result; } catch (Exception ex) { throw ex; } finally { conn.Close(); } } #endregion #region 調用存儲過程 public static int UpdateByProcedure(string procedureName, SqlParameter[] param) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; try { conn.Open(); cmd.CommandType = CommandType.StoredProcedure;//聲明當前要執行的是存儲過程 cmd.CommandText = procedureName;//commandText只需要賦值存儲過程名稱即可 cmd.Parameters.AddRange(param);//添加存儲過程的參數 int result = cmd.ExecuteNonQuery(); return result; } catch (Exception ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 執行多結果查詢(select) /// </summary> /// <param name="sql"></param> /// <returns></returns> public static SqlDataReader GetReaderByProcedure(string procedureName, SqlParameter[] param) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; try { conn.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = procedureName; cmd.Parameters.AddRange(param); SqlDataReader objReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); return objReader; } catch (Exception ex) { conn.Close(); throw ex; } } #endregion #region 啟用事務 /// <summary> /// 啟用事務執行多條SQL語句 /// </summary> /// <param name="sqlList">SQL語句列表</param> /// <returns></returns> public static bool ExecSQLByTran(List<string> sqlList) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; try { conn.Open(); cmd.Transaction = conn.BeginTransaction(); //開啟事務 foreach (string itemSql in sqlList)//循環提交SQL語句 { cmd.CommandText = itemSql; cmd.ExecuteNonQuery(); } cmd.Transaction.Commit(); //提交事務(同時自動清除事務) return true; } catch (Exception ex) { if (cmd.Transaction != null) cmd.Transaction.Rollback();//回滾事務(同時自動清除事務) throw new Exception("調用事務方法時出現錯誤:" + ex.Message); } finally { if (cmd.Transaction != null) cmd.Transaction = null; conn.Close(); } } #endregion } }

SQL-sqlHelper001