ADO.NET 封裝的資料庫幫助類
阿新 • • 發佈:2018-11-05
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace DAL
{
/// <summary>
/// 資料庫幫助類
/// </summary>
public abstract class DatabaseHelper
{
# region 成員變數
/// <summary>
/// 資料庫連線字串
/// </summary>
private static string m_ConnectionString = null;
# endregion
# region 屬性
/// <summary>
/// 獲取資料庫連線字串
/// </summary>
/// <returns></returns>
public string ConnectionString
{
get
{
if (m_ConnectionString == null)
{
m_ConnectionString = ReadConnectionString("ConnectionString");
}
return m_ConnectionString;
}
}
# endregion
# region 建構函式
public DatabaseHelper()
{
m_ConnectionString = ReadConnectionString("ConnectionString" );
}
public DatabaseHelper(string connectionString)
{
m_ConnectionString = connectionString;
}
# endregion
# region 靜態方法
/// <summary>
/// 從配置檔案讀取資料庫連線字串
/// </summary>
/// <param name="configKeyName"></param>
/// <returns></returns>
private static string ReadConnectionString(string configKeyName)
{
string connectionString = "";
object obj = ConfigurationManager.ConnectionStrings[configKeyName];
if (obj != null)
{
connectionString = obj.ToString();
}
return connectionString;
}
/// <summary>
/// 獲取資料庫連線字串
/// </summary>
/// <returns></returns>
public static string GetConnectionString()
{
if (m_ConnectionString == null)
{
m_ConnectionString = ReadConnectionString("ConnectionString");
}
return m_ConnectionString;
}
#endregion
#region 抽象方法
protected abstract object DataReaderToEntity(IDataReader dataReader);
#endregion
#region 資料庫操作方法
/// <summary>
/// 構造資料庫操作命令
/// </summary>
/// <param name="conn">資料庫連線物件</param>
/// <param name="cmdString">sql語句</param>
private SqlCommand CreateCommand(SqlConnection conn, string cmdString)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = cmdString;
return cmd;
}
/// <summary>
/// 清空引數,釋放SqlCommand資源
/// </summary>
/// <param name="cmd"></param>
private void DisposeCommand(SqlCommand cmd)
{
if (cmd != null)
{
cmd.Parameters.Clear();
cmd.Dispose();
}
}
/// <summary>
/// 繫結SqlCommand引數,引數值為空時不新增
/// </summary>
/// <param name="cmd"></param>
/// <param name="parameters"></param>
private void AttachParameters(SqlCommand cmd, params SqlParameter[] parameters)
{
if (parameters != null)
{
foreach (SqlParameter para in parameters)
{
if (para != null)
{
if (para.Direction == ParameterDirection.InputOutput ||
(para.Direction == ParameterDirection.Input && para.Value == null))
{
para.Value = DBNull.Value;
}
cmd.Parameters.Add(para);
}
}
}
}
/// <summary>
/// 執行一個查詢,返回查詢的結果集
/// </summary>
/// <param name="sqlString">sql語句</param>
/// <param name="parameters">引數化引數</param>
/// <returns>查詢的結果集</returns>
public DataTable ExecuteDataTable(string sqlString, params SqlParameter[] parameters)
{
DataTable dataTable = new DataTable();
using (SqlConnection conn = new SqlConnection(m_ConnectionString))
{
conn.Open();
SqlCommand cmd = CreateCommand(conn, sqlString);
AttachParameters(cmd, parameters);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dataTable);
DisposeCommand(cmd);
}
return dataTable;
}
/// <summary>
/// 執行sql操作,返回受影響的行數
/// </summary>
/// <param name="sqlString">sql語句</param>
/// <param name="parameters">引數化引數</param>
/// <returns>受影響的行數</returns>
public int ExecuteNonQuery(string sqlString, params SqlParameter[] parameters)
{
int num = 0;
using (SqlConnection conn = new SqlConnection(m_ConnectionString))
{
conn.Open();
SqlCommand cmd = CreateCommand(conn, sqlString);
AttachParameters(cmd, parameters);
num = cmd.ExecuteNonQuery();
DisposeCommand(cmd);
}
return num;
}
/// <summary>
/// 執行查詢,並返回查詢所返回的結果集中第一行的第一列。忽略其他列或行。
/// </summary>
/// <param name="sqlString">sql語句</param>
/// <param name="parameters">引數化引數</param>
/// <returns>結果集中第一行的第一列;如果結果集為空,則為空引用</returns>
public object ExecuteScalar(string sqlString, params SqlParameter[] parameters)
{
object obj = new object();
using (SqlConnection conn = new SqlConnection(m_ConnectionString))
{
conn.Open();
SqlCommand cmd = CreateCommand(conn, sqlString);
AttachParameters(cmd, parameters);
obj = cmd.ExecuteScalar();
DisposeCommand(cmd);
}
return obj;
}
public SqlDataReader ExecuteReader(string sqlString, params SqlParameter[] parameters)
{
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(m_ConnectionString);
conn.Open();
SqlCommand cmd = CreateCommand(conn, sqlString);
AttachParameters(cmd, parameters);
reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
DisposeCommand(cmd);
return reader;
}
# endregion
}
}