C# DBHelper類 參考
阿新 • • 發佈:2019-02-19
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class SQLHelper
{
//連線字串
static string strConn = ConfigurationManager.ConnectionStrings["CnnhoRechargePlatformConnectionString"].ToString();
#region 執行查詢,返回DataTable物件-----------------------
public static DataTable GetTable(string strSQL)
{
return GetTable(strSQL, null);
}
public static DataTable GetTable(string strSQL, SqlParameter[] pas)
{
return GetTable(strSQL, pas, CommandType.Text);
}
/// <summary>
/// 執行查詢,返回DataTable物件
/// </summary>
/// <param name="strSQL">sql語句</param>
/// <param name="pas">引數陣列</param>
/// <param name="cmdtype">Command型別</param>
/// <returns>DataTable物件</returns>
public static DataTable GetTable(string strSQL, SqlParameter[] pas, CommandType cmdtype)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
da.SelectCommand.CommandType = cmdtype;
if (pas != null)
{
da.SelectCommand.Parameters.AddRange(pas);
}
da.Fill(dt);
}
return dt;
}
#endregion
#region 執行查詢,返回DataSet物件-------------------------
public static DataSet GetDataSet(string strSQL)
{
return GetDataSet(strSQL, null);
}
public static DataSet GetDataSet(string strSQL, SqlParameter[] pas)
{
return GetDataSet(strSQL, pas, CommandType.Text);
}
/// <summary>
/// 執行查詢,返回DataSet物件
/// </summary>
/// <param name="strSQL">sql語句</param>
/// <param name="pas">引數陣列</param>
/// <param name="cmdtype">Command型別</param>
/// <returns>DataSet物件</returns>
public static DataSet GetDataSet(string strSQL, SqlParameter[] pas, CommandType cmdtype)
{
DataSet dt = new DataSet();
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
da.SelectCommand.CommandType = cmdtype;
if (pas != null)
{
da.SelectCommand.Parameters.AddRange(pas);
}
da.Fill(dt);
}
return dt;
}
#endregion
#region 執行非查詢儲存過程和SQL語句-----------------------------
public static int ExcuteProc(string ProcName)
{
return ExcuteSQL(ProcName, null, CommandType.StoredProcedure);
}
public static int ExcuteProc(string ProcName, SqlParameter[] pars)
{
return ExcuteSQL(ProcName, pars, CommandType.StoredProcedure);
}
public static int ExcuteSQL(string strSQL)
{
return ExcuteSQL(strSQL, null);
}
public static int ExcuteSQL(string strSQL, SqlParameter[] paras)
{
return ExcuteSQL(strSQL, paras, CommandType.Text);
}
/// 執行非查詢儲存過程和SQL語句
/// 增、刪、改
/// </summary>
/// <param name="strSQL">要執行的SQL語句</param>
/// <param name="paras">引數列表,沒有引數填入null</param>
/// <param name="cmdType">Command型別</param>
/// <returns>返回影響行數</returns>
public static int ExcuteSQL(string strSQL, SqlParameter[] paras, CommandType cmdType)
{
int i = 0;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = cmdType;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
i = cmd.ExecuteNonQuery();
conn.Close();
}
return i;
}
#endregion
#region 執行查詢返回第一行,第一列---------------------------------
public static int ExcuteScalarSQL(string strSQL)
{
return ExcuteScalarSQL(strSQL, null);
}
public static int ExcuteScalarSQL(string strSQL, SqlParameter[] paras)
{
return ExcuteScalarSQL(strSQL, paras, CommandType.Text);
}
public static int ExcuteScalarProc(string strSQL, SqlParameter[] paras)
{
return ExcuteScalarSQL(strSQL, paras, CommandType.StoredProcedure);
}
/// <summary>
/// 執行SQL語句,返回第一行,第一列
/// </summary>
/// <param name="strSQL">要執行的SQL語句</param>
/// <param name="paras">引數列表,沒有引數填入null</param>
/// <returns>返回影響行數</returns>
public static int ExcuteScalarSQL(string strSQL, SqlParameter[] paras, CommandType cmdType)
{
int i = 0;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = cmdType;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
i = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
}
return i;
}
#endregion
#region 查詢獲取單個值------------------------------------
/// <summary>
/// 呼叫不帶引數的儲存過程獲取單個值
/// </summary>
/// <param name="ProcName"></param>
/// <returns></returns>
public static object GetObjectByProc(string ProcName)
{
return GetObjectByProc(ProcName, null);
}
/// <summary>
/// 呼叫帶引數的儲存過程獲取單個值
/// </summary>
/// <param name="ProcName"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static object GetObjectByProc(string ProcName, SqlParameter[] paras)
{
return GetObject(ProcName, paras, CommandType.StoredProcedure);
}
/// <summary>
/// 根據sql語句獲取單個值
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public static object GetObject(string strSQL)
{
return GetObject(strSQL, null);
}
/// <summary>
/// 根據sql語句 和 引數陣列獲取單個值
/// </summary>
/// <param name="strSQL"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static object GetObject(string strSQL, SqlParameter[] paras)
{
return GetObject(strSQL, paras, CommandType.Text);
}
/// <summary>
/// 執行SQL語句,返回首行首列
/// </summary>
/// <param name="strSQL">要執行的SQL語句</param>
/// <param name="paras">引數列表,沒有引數填入null</param>
/// <returns>返回的首行首列</returns>
public static object GetObject(string strSQL, SqlParameter[] paras, CommandType cmdtype)
{
object o = null;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = cmdtype;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
o = cmd.ExecuteScalar();
conn.Close();
}
return o;
}
#endregion
#region 查詢獲取DataReader------------------------------------
/// <summary>
/// 呼叫不帶引數的儲存過程,返回DataReader物件
/// </summary>
/// <param name="procName">儲存過程名稱</param>
/// <returns>DataReader物件</returns>
public static SqlDataReader GetReaderByProc(string procName)
{
return GetReaderByProc(procName, null);
}
/// <summary>
/// 呼叫帶有引數的儲存過程,返回DataReader物件
/// </summary>
/// <param name="procName">儲存過程名</param>
/// <param name="paras">引數陣列</param>
/// <returns>DataReader物件</returns>
public static SqlDataReader GetReaderByProc(string procName, SqlParameter[] paras)
{
return GetReader(procName, paras, CommandType.StoredProcedure);
}
/// <summary>
/// 根據sql語句返回DataReader物件
/// </summary>
/// <param name="strSQL">sql語句</param>
/// <returns>DataReader物件</returns>
public static SqlDataReader GetReader(string strSQL)
{
return GetReader(strSQL, null);
}
/// <summary>
/// 根據sql語句和引數返回DataReader物件
/// </summary>
/// <param name="strSQL">sql語句</param>
/// <param name="paras">引數陣列</param>
/// <returns>DataReader物件</returns>
public static SqlDataReader GetReader(string strSQL, SqlParameter[] paras)
{
return GetReader(strSQL, paras, CommandType.Text);
}
/// <summary>
/// 查詢SQL語句獲取DataReader
/// </summary>
/// <param name="strSQL">查詢的SQL語句</param>
/// <param name="paras">引數列表,沒有引數填入null</param>
/// <returns>查詢到的DataReader(關閉該物件的時候,自動關閉連線)</returns>
public static SqlDataReader GetReader(string strSQL, SqlParameter[] paras, CommandType cmdtype)
{
SqlDataReader sqldr = null;
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = cmdtype;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
//CommandBehavior.CloseConnection的作用是如果關聯的DataReader物件關閉,則連線自動關閉
sqldr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return sqldr;
}
#endregion
#region 批量插入資料---------------------------------------------
/// <summary>
/// 往資料庫中批量插入資料
/// </summary>
/// <param name="sourceDt">資料來源表</param>
/// <param name="targetTable">伺服器上目標表</param>
public static void BulkToDB(DataTable sourceDt, string targetTable)
{
SqlConnection conn = new SqlConnection(strConn);
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);
//用其它源的資料有效批量載入sql server表中
bulkCopy.DestinationTableName = targetTable;
//伺服器上目標表的名稱
bulkCopy.BatchSize = sourceDt.Rows.Count; //每一批次中的行數
try
{
conn.Open();
if (sourceDt != null && sourceDt.Rows.Count != 0)
bulkCopy.WriteToServer(sourceDt); //將提供的資料來源中的所有行復制到目標表中
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
if (bulkCopy != null)
bulkCopy.Close();
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class SQLHelper
{
//連線字串
static string strConn = ConfigurationManager.ConnectionStrings["CnnhoRechargePlatformConnectionString"].ToString();
#region 執行查詢,返回DataTable物件-----------------------
public static DataTable GetTable(string strSQL)
{
return GetTable(strSQL, null);
}
public static DataTable GetTable(string strSQL, SqlParameter[] pas)
{
return GetTable(strSQL, pas, CommandType.Text);
}
/// <summary>
/// 執行查詢,返回DataTable物件
/// </summary>
/// <param name="strSQL">sql語句</param>
/// <param name="pas">引數陣列</param>
/// <param name="cmdtype">Command型別</param>
/// <returns>DataTable物件</returns>
public static DataTable GetTable(string strSQL, SqlParameter[] pas, CommandType cmdtype)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
da.SelectCommand.CommandType = cmdtype;
if (pas != null)
{
da.SelectCommand.Parameters.AddRange(pas);
}
da.Fill(dt);
}
return dt;
}
#endregion
#region 執行查詢,返回DataSet物件-------------------------
public static DataSet GetDataSet(string strSQL)
{
return GetDataSet(strSQL, null);
}
public static DataSet GetDataSet(string strSQL, SqlParameter[] pas)
{
return GetDataSet(strSQL, pas, CommandType.Text);
}
/// <summary>
/// 執行查詢,返回DataSet物件
/// </summary>
/// <param name="strSQL">sql語句</param>
/// <param name="pas">引數陣列</param>
/// <param name="cmdtype">Command型別</param>
/// <returns>DataSet物件</returns>
public static DataSet GetDataSet(string strSQL, SqlParameter[] pas, CommandType cmdtype)
{
DataSet dt = new DataSet();
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
da.SelectCommand.CommandType = cmdtype;
if (pas != null)
{
da.SelectCommand.Parameters.AddRange(pas);
}
da.Fill(dt);
}
return dt;
}
#endregion
#region 執行非查詢儲存過程和SQL語句-----------------------------
public static int ExcuteProc(string ProcName)
{
return ExcuteSQL(ProcName, null, CommandType.StoredProcedure);
}
public static int ExcuteProc(string ProcName, SqlParameter[] pars)
{
return ExcuteSQL(ProcName, pars, CommandType.StoredProcedure);
}
public static int ExcuteSQL(string strSQL)
{
return ExcuteSQL(strSQL, null);
}
public static int ExcuteSQL(string strSQL, SqlParameter[] paras)
{
return ExcuteSQL(strSQL, paras, CommandType.Text);
}
/// 執行非查詢儲存過程和SQL語句
/// 增、刪、改
/// </summary>
/// <param name="strSQL">要執行的SQL語句</param>
/// <param name="paras">引數列表,沒有引數填入null</param>
/// <param name="cmdType">Command型別</param>
/// <returns>返回影響行數</returns>
public static int ExcuteSQL(string strSQL, SqlParameter[] paras, CommandType cmdType)
{
int i = 0;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = cmdType;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
i = cmd.ExecuteNonQuery();
conn.Close();
}
return i;
}
#endregion
#region 執行查詢返回第一行,第一列---------------------------------
public static int ExcuteScalarSQL(string strSQL)
{
return ExcuteScalarSQL(strSQL, null);
}
public static int ExcuteScalarSQL(string strSQL, SqlParameter[] paras)
{
return ExcuteScalarSQL(strSQL, paras, CommandType.Text);
}
public static int ExcuteScalarProc(string strSQL, SqlParameter[] paras)
{
return ExcuteScalarSQL(strSQL, paras, CommandType.StoredProcedure);
}
/// <summary>
/// 執行SQL語句,返回第一行,第一列
/// </summary>
/// <param name="strSQL">要執行的SQL語句</param>
/// <param name="paras">引數列表,沒有引數填入null</param>
/// <returns>返回影響行數</returns>
public static int ExcuteScalarSQL(string strSQL, SqlParameter[] paras, CommandType cmdType)
{
int i = 0;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = cmdType;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
i = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
}
return i;
}
#endregion
#region 查詢獲取單個值------------------------------------
/// <summary>
/// 呼叫不帶引數的儲存過程獲取單個值
/// </summary>
/// <param name="ProcName"></param>
/// <returns></returns>
public static object GetObjectByProc(string ProcName)
{
return GetObjectByProc(ProcName, null);
}
/// <summary>
/// 呼叫帶引數的儲存過程獲取單個值
/// </summary>
/// <param name="ProcName"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static object GetObjectByProc(string ProcName, SqlParameter[] paras)
{
return GetObject(ProcName, paras, CommandType.StoredProcedure);
}
/// <summary>
/// 根據sql語句獲取單個值
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public static object GetObject(string strSQL)
{
return GetObject(strSQL, null);
}
/// <summary>
/// 根據sql語句 和 引數陣列獲取單個值
/// </summary>
/// <param name="strSQL"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static object GetObject(string strSQL, SqlParameter[] paras)
{
return GetObject(strSQL, paras, CommandType.Text);
}
/// <summary>
/// 執行SQL語句,返回首行首列
/// </summary>
/// <param name="strSQL">要執行的SQL語句</param>
/// <param name="paras">引數列表,沒有引數填入null</param>
/// <returns>返回的首行首列</returns>
public static object GetObject(string strSQL, SqlParameter[] paras, CommandType cmdtype)
{
object o = null;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = cmdtype;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
o = cmd.ExecuteScalar();
conn.Close();
}
return o;
}
#endregion
#region 查詢獲取DataReader------------------------------------
/// <summary>
/// 呼叫不帶引數的儲存過程,返回DataReader物件
/// </summary>
/// <param name="procName">儲存過程名稱</param>
/// <returns>DataReader物件</returns>
public static SqlDataReader GetReaderByProc(string procName)
{
return GetReaderByProc(procName, null);
}
/// <summary>
/// 呼叫帶有引數的儲存過程,返回DataReader物件
/// </summary>
/// <param name="procName">儲存過程名</param>
/// <param name="paras">引數陣列</param>
/// <returns>DataReader物件</returns>
public static SqlDataReader GetReaderByProc(string procName, SqlParameter[] paras)
{
return GetReader(procName, paras, CommandType.StoredProcedure);
}
/// <summary>
/// 根據sql語句返回DataReader物件
/// </summary>
/// <param name="strSQL">sql語句</param>
/// <returns>DataReader物件</returns>
public static SqlDataReader GetReader(string strSQL)
{
return GetReader(strSQL, null);
}
/// <summary>
/// 根據sql語句和引數返回DataReader物件
/// </summary>
/// <param name="strSQL">sql語句</param>
/// <param name="paras">引數陣列</param>
/// <returns>DataReader物件</returns>
public static SqlDataReader GetReader(string strSQL, SqlParameter[] paras)
{
return GetReader(strSQL, paras, CommandType.Text);
}
/// <summary>
/// 查詢SQL語句獲取DataReader
/// </summary>
/// <param name="strSQL">查詢的SQL語句</param>
/// <param name="paras">引數列表,沒有引數填入null</param>
/// <returns>查詢到的DataReader(關閉該物件的時候,自動關閉連線)</returns>
public static SqlDataReader GetReader(string strSQL, SqlParameter[] paras, CommandType cmdtype)
{
SqlDataReader sqldr = null;
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = cmdtype;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
//CommandBehavior.CloseConnection的作用是如果關聯的DataReader物件關閉,則連線自動關閉
sqldr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return sqldr;
}
#endregion
#region 批量插入資料---------------------------------------------
/// <summary>
/// 往資料庫中批量插入資料
/// </summary>
/// <param name="sourceDt">資料來源表</param>
/// <param name="targetTable">伺服器上目標表</param>
public static void BulkToDB(DataTable sourceDt, string targetTable)
{
SqlConnection conn = new SqlConnection(strConn);
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);
//用其它源的資料有效批量載入sql server表中
bulkCopy.DestinationTableName = targetTable;
//伺服器上目標表的名稱
bulkCopy.BatchSize = sourceDt.Rows.Count; //每一批次中的行數
try
{
conn.Open();
if (sourceDt != null && sourceDt.Rows.Count != 0)
bulkCopy.WriteToServer(sourceDt); //將提供的資料來源中的所有行復制到目標表中
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
if (bulkCopy != null)
bulkCopy.Close();
}
}
#endregion
}
}