Small and medium system. net DataAccess data category

Source: Internet
Author: User

 

# 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.