# Region reference object
Using System;
Using System. Xml;
Using System. Data;
Using System. Data. SqlClient;
Using System. Web;
# Endregion
Namespace SysClassLibrary
{
/// <Summary>
/// Summary of DataAccess.
/// <Author> wuchen </author>
/// <Date> 2004-4-12 </date>
/// <Email> scwuchen@263.net </email>
/// <Description> data processing base class. Call method: DataAccess. dataSet (string) sqlstr); or DataAccess. dataSet (string) sqlstr, ref DataSet ds); </description>
/// </Summary>
Public class DataAccess
{
# Region attributes
/// <Summary>
/// Whether the database connection must be closed
/// </Summary>
Public static bool mustCloseConnection
{
Get
{
Return _ mustCloseConnection;
}
Set
{
_ MustCloseConnection = value;
}
}
/// <Summary>
/// Connection string
/// </Summary>
Public static string connectionString
{
Get
{
If (_ connectionString = string. Empty)
Return SysConfig. ConnectionString;
Else
Return _ connectionString;
}
Set
{
_ ConnectionString = value;
}
}
/// <Summary>
/// Whether to close the database connection
/// </Summary>
Private static bool _ mustCloseConnection = true;
Private static string _ connectionString = string. Empty;
# Endregion
# Region class Constructor
/// <Summary>
/// Constructor
/// </Summary>
Public DataAccess ()
{
}
/// <Summary>
/// Destructor to release the corresponding object
/// </Summary>
~ DataAccess ()
{
}
# Endregion
# Region Method
/// <Summary>
/// Execute the SQL query statement
/// </Summary>
/// <Param name = "sqlstr"> input SQL statement </param>
/// <Returns> I </returns>
Public static int ExecuteSql (string sqlstr ){
Int I = 0;
Using (SqlConnection conn = new SqlConnection (connectionString ))
{
SqlCommand comm = new SqlCommand ();
Comm. Connection = conn;
Comm. CommandType = CommandType. Text;
Comm. CommandText = sqlstr;
Try
{
Conn. Open ();
I = comm. ExecuteNonQuery ();
}
Catch (SqlException e)
{
New ErrorLog (). SaveDataAccessError (e );
}
Finally
{
Conn. Close ();
Comm. Dispose ();
}
}
Return I;
}
/// <Summary>
/// Execute the Stored Procedure
/// </Summary>
/// <Param name = "procName"> stored procedure name </param>
/// <Param name = "coll"> SqlParameters set </param>
Public static void ExecutePorcedure (string procName, SqlParameter [] coll)
{
Using (SqlConnection conn = new SqlConnection (connectionString ))
{
SqlCommand comm = new SqlCommand ();
Comm. Connection = conn;
Comm. CommandType = CommandType. StoredProcedure;
ExecutePorcedure (procName, coll, conn, comm );
}
}
Public static void ExecutePorcedure (string procName, SqlParameter [] coll, ref DataSet ds)
{
Using (SqlConnection conn = new SqlConnection (connectionString ))
{
SqlCommand comm = new SqlCommand ();
Comm. Connection = conn;
Comm. CommandType = CommandType. StoredProcedure;
ExecutePorcedure (procName, coll, conn, comm, ref ds );
}
}
/// <Summary>
/// Execute the Stored Procedure class
/// </Summary>
/// <Param name = "procName"> </param>
/// <Param name = "coll"> </param>
/// <Param name = "conn"> </param>
/// <Param name = "comm"> </param>
Public static void ExecutePorcedure (string procName, SqlParameter [] coll, SqlConnection conn, SqlCommand comm)
{
If (procName = null | procName = "")
Throw new SqlNullException ();
Try
{
Conn. Open ();
For (int I = 0; I <coll. Length; I ++)
{
Comm. Parameters. Add (coll [I]);
}
Comm. CommandType = CommandType. StoredProcedure;
Comm. CommandText = procName;
Comm. ExecuteNonQuery ();
}
Catch (SqlException e)
{
New ErrorLog (). SaveDataAccessError (e );
}
Finally
{
Comm. Parameters. Clear ();
Conn. Close ();
Comm. Dispose ();
}
}
Public static void ExecutePorcedure (string procName, SqlParameter [] coll, SqlConnection conn, SqlCommand comm, ref DataSet ds)
{
If (procName = null | procName = "")
Throw new SqlNullException ();
Try
{
SqlDataAdapter da = new SqlDataAdapter ();
Conn. Open ();
For (int I = 0; I <coll. Length; I ++)
{
Comm. Parameters. Add (coll [I]);
}
Comm. CommandType = CommandType. StoredProcedure;
Comm. CommandText = procName;
Da. SelectCommand = comm;
Da. Fill (ds );
}
Catch (SqlException e)
{
New ErrorLog (). SaveDataAccessError (e );
}
Finally
{
Comm. Parameters. Clear ();
Conn. Close ();
Comm. Dispose ();
}
}
/// <Summary