熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ADO.NET的一個幫助類庫

2022-06-13   來源: .NET編程 

  ADONET的一個幫助類庫只須一個連接一條SQL語句輕松實現數據庫的查旬再加一個object 輕松實現數據庫的增刪改

  該類庫根據是否需要事務可分為兩個部分分別由三個構造方法決定

  命名空間DBAssistant類DBAssist

  方法ExecNoQueryWithTran() 實現帶事務的增刪改返回(int)影響的行數

  方法 ExecNoQuery()實現不帶事務的增刪改返回(int) 影響的行數

  方法ExecScalar() 實現返回首行首列的查詢返回(object)

  方法ExecSelect() 返回SqlDataReader

  方法GetDataSet() 返回DataSet

  using System;
using SystemCollectionsGeneric;
using SystemText;
using SystemData;
using SystemDataSqlClient;
using WriteException;
namespace DBAssistant
{
public class DBAssist
{
/// <summary>
/// 重載的構造方法
/// </summary>
public DBAssist() { }
public DBAssist(string strSqlSqlConnection conn)
{
thisStrSql = strSql;
thisConn = conn;
}
public DBAssist(string strSqlobject objSqlConnection conn)
{
thisStrSql = strSql;
thisObj = obj;
thisConn = conn;
}
public DBAssist(string strSql object obj SqlConnection connSqlTransaction sqlTran)
{
thisStrSql = strSql;
thisObj = obj;
thisConn = conn;
thissqlTran = sqlTran;
}
/// <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 conn))
{
cmdTransaction = sqlTran;
num = cmdExecuteNonQuery();
//SqlTranCommit();
}
}
catch (Exception ex)
{
WriExceptionWrite(ex);
//SqlTranRollback();
}
return num;
}
/// <summary>
/// 不帶事務的增刪改方法
/// </summary>
/// <returns>int</returns>
public int ExecNoQuery()
{
int num = ;
try
{
using (SqlCommand cmd = new SqlCommand(strSql conn))
{
num = cmdExecuteNonQuery();
}
}
catch (Exception ex)
{
WriExceptionWrite(ex);
}
return num;
}
/// <summary>
/// 返回 SqlDataReader 的查詢方法
/// </summary>
/// <returns>SqlDataReader</returns>
public SqlDataReader ExecSelect()
{
SqlDataReader dr = null;
try
{
SqlCommand cmd = new SqlCommand(strSql conn);
dr = cmdExecuteReader(CommandBehaviorCloseConnection);
return dr;
}
catch (Exception ex)
{
WriExceptionWrite(ex);
}
return null;
}
/// <summary>
/// 調用ExecuteScalar()方法返回 object 的查詢方法
/// </summary>
/// <returns>Object</returns>
public object ExecScalar()
{
count=null;
try
{
using (SqlCommand cmd = new SqlCommand(strSql conn))
{
count = cmdExecuteScalar();
}
}
catch (Exception ex)
{
WriExceptionWrite(ex);
}
return count;
}
DataSet ds;
/// <summary>
/// 返回DataSet的查詢方法
/// </summary>
/// <returns>DataSet</returns>
public DataSet GetDataSet()
{
ds = null;
using (SqlDataAdapter sda = new SqlDataAdapter(strSql conn))
{
using (ds = new DataSet())
{
sdaFill(ds);
}
}
return ds;
}

  }
}


From:http://tw.wingwit.com/Article/program/net/201311/12853.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.