ADO
該類庫根據是否需要事務可分為兩個部分
命名空間DBAssistant
方法ExecNoQueryWithTran() 實現帶事務的增刪改
方法 ExecNoQuery()實現不帶事務的增刪改
方法ExecScalar() 實現返回首行首列的查詢
方法ExecSelect() 返回SqlDataReader
方法GetDataSet() 返回DataSet
using System;
using System
using System
using System
using System
using WriteException;
namespace DBAssistant
{
public class DBAssist
{
/// <summary>
/// 重載的構造方法
/// </summary>
public DBAssist() { }
public DBAssist(string strSql
{
this
this
}
public DBAssist(string strSql
{
this
this
this
}
public DBAssist(string strSql
{
this
this
this
this
}
/// <summary>
/// sql語句
/// </summary>
string strSql;
public string StrSql
{
get { return strSql; }
set { strSql = value; }
}
/// <summary>
/// 要操作的對象
/// </summary>
object obj;
public object Obj
{
get { return obj; }
set { obj = value; }
}
/// <summary>
/// 連接
/// </summary>
SqlConnection conn;
public SqlConnection Conn
{
get { return conn; }
set { conn = value; }
}
/// <summary>
/// 事務
/// </summary>
SqlTransaction sqlTran;
public SqlTransaction SqlTran
{
get { return sqlTran; }
set { sqlTran = value; }
}
public object count;
/// <summary>
/// 帶事務增刪改方法
/// </summary>
/// <returns>int</returns>
public int ExecNoQueryWithTran()
{
int num =
try
{
using (SqlCommand cmd = new SqlCommand(strSql
{
cmd
num = cmd
//SqlTran
}
}
catch (Exception ex)
{
WriException
//SqlTran
}
return num;
}
/// <summary>
/// 不帶事務的增刪改方法
/// </summary>
/// <returns>int</returns>
public int ExecNoQuery()
{
int num =
try
{
using (SqlCommand cmd = new SqlCommand(strSql
{
num = cmd
}
}
catch (Exception ex)
{
WriException
}
return num;
}
/// <summary>
/// 返回 SqlDataReader 的查詢方法
/// </summary>
/// <returns>SqlDataReader</returns>
public SqlDataReader ExecSelect()
{
SqlDataReader dr = null;
try
{
SqlCommand cmd = new SqlCommand(strSql
dr = cmd
return dr;
}
catch (Exception ex)
{
WriException
}
return null;
}
/// <summary>
/// 調用ExecuteScalar()方法
/// </summary>
/// <returns>Object</returns>
public object ExecScalar()
{
count=null;
try
{
using (SqlCommand cmd = new SqlCommand(strSql
{
count = cmd
}
}
catch (Exception ex)
{
WriException
}
return count;
}
DataSet ds;
/// <summary>
/// 返回DataSet的查詢方法
/// </summary>
/// <returns>DataSet</returns>
public DataSet GetDataSet()
{
ds = null;
using (SqlDataAdapter sda = new SqlDataAdapter(strSql
{
using (ds = new DataSet())
{
sda
}
}
return ds;
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/12853.html