|
using System.Data;
using System.Data.SqlClient;
namespace SysClassLibrary
{
/// <summary>
/// DataAccess 的摘要說(shuō)明。
/// <description>數(shù)據(jù)處理基類,調(diào)用方式:DataAccess.DataSet((string)sqlstr);或者DataAccess.DataSet((string)sqlstr,ref DataSet ds); </description>
/// </summary>
public class DataAccess
{
#region 屬性
protected static SqlConnection conn=new SqlConnection();
protected static SqlCommand comm=new SqlCommand();
#endregion
public DataAccess()
{
//init();
}
#region 內(nèi)部函數(shù) 靜態(tài)方法中不會(huì)執(zhí)行DataAccess()構(gòu)造函數(shù)
/// <summary>
/// 打開數(shù)據(jù)庫(kù)連接
/// </summary>
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
//SysConfig.ConnectionString 為系統(tǒng)配置類中連接字符串,如:"server=localhost;database=databasename;uid=sa;pwd=;"
conn.ConnectionString = SysConfig.ConnectionString ;
comm.Connection =conn;
try
{
conn.Open();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
}
}
/// <summary>
/// 關(guān)閉當(dāng)前數(shù)據(jù)庫(kù)連接
/// </summary>
private static void closeConnection()
{
if(conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
comm.Dispose();
}
#endregion
/// <summary>
/// 執(zhí)行Sql查詢語(yǔ)句
/// </summary>
/// <param name="sqlstr">傳入的Sql語(yǔ)句</param>
public static void ExecuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType =CommandType.Text ;
comm.CommandText =sqlstr;
comm.ExecuteNonQuery();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 執(zhí)行存儲(chǔ)過(guò)程
/// </summary>
/// <param name="procName">存儲(chǔ)過(guò)程名</param>
/// <param name="coll">SqlParameters 集合</param>
public static void ExecutePorcedure(string procName,SqlParameter[] coll)
{
try
{
openConnection();
for(int i=0;i<coll.Length;i++)
{
comm.Parameters .Add(coll);
}
comm.CommandType=CommandType.StoredProcedure ;
comm.CommandText =procName;
comm.ExecuteNonQuery();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
comm.Parameters.Clear();
closeConnection();
}
}
/// <summary>
/// 執(zhí)行存儲(chǔ)過(guò)程并返回?cái)?shù)據(jù)集
/// </summary>
/// <param name="procName">存儲(chǔ)過(guò)程名稱</param>
/// <param name="coll">SqlParameter集合</param>
/// <param name="ds">DataSet </param>
public static void ExecutePorcedure(string procName,SqlParameter[] coll,ref DataSet ds)
{
try
{
SqlDataAdapter da=new SqlDataAdapter();
openConnection();
for(int i=0;i<coll.Length;i++)
{
comm.Parameters .Add(coll);
}
comm.CommandType=CommandType.StoredProcedure ;
comm.CommandText =procName;
da.SelectCommand =comm;
da.Fill(ds);
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
comm.Parameters.Clear();
closeConnection();
}
}
/// <summary>
/// 執(zhí)行Sql查詢語(yǔ)句并返回第一行的第一條記錄,返回值為object 使用時(shí)需要拆箱操作 -> Unbox
/// </summary>
/// <param name="sqlstr">傳入的Sql語(yǔ)句</param>
/// <returns>object 返回值 </returns>
public static object ExecuteScalar(string sqlstr)
{
object obj=new object();
try
{
openConnection();
comm.CommandType =CommandType.Text ;
comm.CommandText =sqlstr;
obj=comm.ExecuteScalar();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return obj;
}
/// <summary>
/// 執(zhí)行Sql查詢語(yǔ)句,同時(shí)進(jìn)行事務(wù)處理
/// </summary>
/// <param name="sqlstr">傳入的Sql語(yǔ)句</param>
public static void ExecuteSqlWithTransaction(string sqlstr)
{
SqlTransaction trans ;
trans=conn.BeginTransaction();
comm.Transaction =trans;
try
{
openConnection();
comm.CommandType =CommandType.Text ;
comm.CommandText =sqlstr;
comm.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
}
finally
{
closeConnection();
}
}
/// <summary>
/// 返回指定Sql語(yǔ)句的SqlDataReader,請(qǐng)注意,在使用后請(qǐng)關(guān)閉本對(duì)象,同時(shí)將自動(dòng)調(diào)用closeConnection()來(lái)關(guān)閉數(shù)據(jù)庫(kù)連接
/// 方法關(guān)閉數(shù)據(jù)庫(kù)連接
/// </summary>
/// <param name="sqlstr">傳入的Sql語(yǔ)句</param>
/// <returns>SqlDataReader對(duì)象</returns>
public static SqlDataReader dataReader(string sqlstr)
{
SqlDataReader dr=null;
try
{
openConnection();
comm.CommandText =sqlstr;
comm.CommandType =CommandType.Text ;
dr=comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
dr.Close();
closeConnection();
}
catch
{
}
}
return dr;
}
/// <summary>
/// 返回指定Sql語(yǔ)句的SqlDataReader,請(qǐng)注意,在使用后請(qǐng)關(guān)閉本對(duì)象,同時(shí)將自動(dòng)調(diào)用closeConnection()來(lái)關(guān)閉數(shù)據(jù)庫(kù)連接
/// 方法關(guān)閉數(shù)據(jù)庫(kù)連接
/// </summary>
/// <param name="sqlstr">傳入的Sql語(yǔ)句</param>
/// <param name="dr">傳入的ref DataReader 對(duì)象</param>
public static void dataReader(string sqlstr,ref SqlDataReader dr)
{
try
{
openConnection();
comm.CommandText =sqlstr;
comm.CommandType =CommandType.Text ;
dr=comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
if(dr!=null && !dr.IsClosed)
dr.Close();
}
catch
{
}
finally
{
closeConnection();
}
}
}
/// <summary>
/// 返回指定Sql語(yǔ)句的DataSet
/// </summary>
/// <param name="sqlstr">傳入的Sql語(yǔ)句</param>
/// <returns>DataSet</returns>
public static DataSet dataSet(string sqlstr)
{
DataSet ds= new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
try
{
openConnection();
comm.CommandType =CommandType.Text ;
comm.CommandText =sqlstr;
da.SelectCommand =comm;
da.Fill(ds);
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return ds;
}
/// <summary>
/// 返回指定Sql語(yǔ)句的DataSet
/// </summary>
/// <param name="sqlstr">傳入的Sql語(yǔ)句</param>
/// <param name="ds">傳入的引用DataSet對(duì)象</param>
public static void dataSet(string sqlstr,ref DataSet ds)
{
SqlDataAdapter da=new SqlDataAdapter();
try
{
openConnection();
comm.CommandType =CommandType.Text ;
comm.CommandText =sqlstr;
da.SelectCommand =comm;
da.Fill(ds);
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 返回指定Sql語(yǔ)句的DataTable
/// </summary>
/// <param name="sqlstr">傳入的Sql語(yǔ)句</param>
/// <returns>DataTable</returns>
public static DataTable dataTable(string sqlstr)
{
SqlDataAdapter da=new SqlDataAdapter();
DataTable datatable=new DataTable();
try
{
openConnection();
comm.CommandType =CommandType.Text ;
comm.CommandText =sqlstr;
da.SelectCommand =comm;
da.Fill(datatable);
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return datatable;
}
/// <summary>
/// 執(zhí)行指定Sql語(yǔ)句,同時(shí)給傳入DataTable進(jìn)行賦值
/// </summary>
/// <param name="sqlstr">傳入的Sql語(yǔ)句</param>
/// <param name="dt">ref DataTable dt </param>
public static void dataTable(string sqlstr,ref DataTable dt)
{
SqlDataAdapter da=new SqlDataAdapter();
try
{
openConnection();
comm.CommandType =CommandType.Text ;
comm.CommandText =sqlstr;
da.SelectCommand =comm;
da.Fill(dt);
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 執(zhí)行帶參數(shù)存儲(chǔ)過(guò)程并返回?cái)?shù)據(jù)集合
/// </summary>
/// <param name="procName">存儲(chǔ)過(guò)程名稱</param>
/// <param name="parameters">SqlParameterCollection 輸入?yún)?shù)</param>
/// <returns></returns>
public static DataTable dataTable(string procName,SqlParameterCollection parameters)
{
SqlDataAdapter da=new SqlDataAdapter();
DataTable datatable=new DataTable();
try
{
openConnection();
comm.Parameters.Clear();
comm.CommandType=CommandType.StoredProcedure ;
comm.CommandText =procName;
foreach(SqlParameter para in parameters)
{
SqlParameter p=(SqlParameter)para;
comm.Parameters.Add(p);
}
da.SelectCommand =comm;
da.Fill(datatable);
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return datatable;
}
public static DataView dataView(string sqlstr)
{
SqlDataAdapter da=new SqlDataAdapter();
DataView dv=new DataView();
DataSet ds=new DataSet();
try
{
openConnection();
comm.CommandType=CommandType.Text;
comm.CommandText =sqlstr;
da.SelectCommand =comm;
da.Fill(ds);
dv=ds.Tables[0].DefaultView;
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dv;
}
}
}
AspNet技術(shù):ASP.NET 數(shù)據(jù)訪問(wèn)類,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。