asp.net database connection Class Code (SQL) _ Practical Tips

Source: Internet
Author: User
Tags first row
Copy Code code as follows:

public class Sqloperation
{
#region Properties
<summary>
The connection string saved in the web.config
</summary>
protected static string connectionstring = system.configuration.configurationmanager.connectionstrings["Hao". ConnectionString;
<summary>
SqlConnection objects
</summary>
protected static SqlConnection conn = new SqlConnection ();
<summary>
SqlCommand objects
</summary>
protected static SqlCommand comm = new SqlCommand ();
#endregion

#region Internal functions
<summary>
Open a database connection
</summary>
private static void ConnectionOpen ()
{
IF (Conn. State!= ConnectionState.Open)
{
Conn. Close ();
Conn. ConnectionString = ConnectionString;
Comm. Connection = conn;
Try
{
Conn. Open ();
}
catch (Exception ex)
{
throw new Exception (ex. message);
}
}
}

<summary>
To close a database connection
</summary>
private static void Connectionclose ()
{
Conn. Close ();
Conn. Dispose ();
Comm. Dispose ();
}

#endregion

<summary>
Execute SQL statement
</summary>
<param name= "SqlString" > SQL statement to execute </param>
public static void ExecuteSQL (String sqlstring)
{
Try
{
ConnectionOpen ();
Comm.commandtype = CommandType.Text;
Comm.commandtext = SqlString;
Comm. ExecuteNonQuery ();
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
}

<summary>
Executing stored procedures
</summary>
<param name= "procedurename" > Stored procedure name </param>
<param name= "Coll" > Stored procedures required parameters set </param>
public static void Executeprocedure (String procedurename, params sqlparameter[] coll)
{
Try
{
ConnectionOpen ();
Comm.commandtype = CommandType.StoredProcedure;
Comm.commandtext = procedurename;
Comm. Parameters.clear ();
for (int i = 0; i < Coll. Length; i++)
{
Comm. Parameters.Add (Coll[i]);
}
Comm. ExecuteNonQuery ();
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
}

<summary>
Executes the SQL query and returns the first record of the first row, returning object, which needs to be disassembled when used-> unbox
</summary>
<param name= "SQLSTR" > Incoming SQL statements </param>
<returns> returns the first line of type Object first record </returns>
public static Object ExecuteScalar (String sqlstring)
{
Object obj = new Object ();
Try
{
ConnectionOpen ();
Comm.commandtype = CommandType.Text;
Comm.commandtext = SqlString;
obj = Comm. ExecuteScalar ();
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
return obj;
}

<summary>
Execute the SQL statement while transaction processing
</summary>
<param name= "SQLSTR" > SQL statement to execute </param>
public static void Executetransactionsql (String sqlstring)
{
SqlTransaction Trans;
trans = conn. BeginTransaction ();
Comm. Transaction = trans;
Try
{
ConnectionOpen ();
Comm.commandtype = CommandType.Text;
Comm.commandtext = SqlString;
Comm. ExecuteNonQuery ();
Trans.commit ();
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
}

<summary>
Executes the specified SQL query, returning the dataset
</summary>
<param name= "SQLSTR" > SQL statement to execute </param>
<returns>DataSet</returns>
public static DataSet Getdatasetbysql (string sqlstring)
{
SqlDataAdapter da = new SqlDataAdapter ();
DataSet ds = new DataSet ();
Try
{
ConnectionOpen ();
Comm.commandtype = CommandType.Text;
Comm.commandtext = SqlString;
Da. SelectCommand = comm;
Da. Fill (DS);
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
return DS;
}

<summary>
Returning a dataset through a stored procedure
</summary>
<param name= "procedurename" > Stored procedure name </param>
<param name= "Coll" >sqlparameter collection </param>
<returns>DataSet</returns>
public static DataSet Getdatasetbyprocedure (string procedurename, params sqlparameter[] coll)
{
SqlDataAdapter da = new SqlDataAdapter ();
DataSet ds = new DataSet ();
Try
{
ConnectionOpen ();
Comm.commandtype = CommandType.StoredProcedure;
Comm. Parameters.clear ();
for (int i = 0; i < Coll. Length; i++)
{
Comm. Parameters.Add (Coll[i]);
}
Comm.commandtext = procedurename;
Da. SelectCommand = comm;
Da. Fill (DS);
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
return DS;
}


<summary>
Returning a dataset through a stored procedure
</summary>
<param name= "procedurename" > Stored procedure name </param>
<returns>DataSet</returns>
public static DataSet Getdatasetbyprocedure (String procedurename)
{
SqlDataAdapter da = new SqlDataAdapter ();
DataSet ds = new DataSet ();
Try
{
ConnectionOpen ();
Comm.commandtype = CommandType.StoredProcedure;
Comm.commandtext = procedurename;
Comm. Parameters.clear ();
Da. SelectCommand = comm;
Da. Fill (DS);
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
return DS;
}

<summary>
Returns a datatable of the specified SQL statement
</summary>
<param name= "SQLSTR" > Incoming SQL statements </param>
<returns>DataTable</returns>
public static DataTable Getdatatablebysql (string sqlstring)
{
SqlDataAdapter da = new SqlDataAdapter ();
DataTable dt = new DataTable ();
Try
{
ConnectionOpen ();
Comm.commandtype = CommandType.Text;
Comm.commandtext = SqlString;
Da. SelectCommand = comm;
Da. Fill (DT);
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
return DT;
}

<summary>
Returning a DataTable based on a stored procedure
</summary>
<param name= "procedurename" > Stored procedure name </param>
<param name= "Coll" >sqlparameter collection </param>
<returns>DataTable</returns>
public static DataTable Getdatatablebyprocedure (string procedurename, params sqlparameter[] coll)
{
SqlDataAdapter da = new SqlDataAdapter ();
DataTable dt = new DataTable ();
Try
{
ConnectionOpen ();
Comm. Parameters.clear ();
Comm.commandtype = CommandType.StoredProcedure;
Comm.commandtext = procedurename;
for (int i = 0; i < Coll. Length; i++)
{
Comm. Parameters.Add (Coll[i]);
}
Da. SelectCommand = comm;
Da. Fill (DT);
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
return DT;
}

<summary>
Returning a DataTable based on a stored procedure
</summary>
<param name= "procedurename" > Stored procedure name </param>
<returns>DataTable</returns>
public static DataTable Getdatatablebyprocedure (String procedurename)
{
SqlDataAdapter da = new SqlDataAdapter ();
DataTable dt = new DataTable ();
Try
{
ConnectionOpen ();
Comm. Parameters.clear ();
Comm.commandtype = CommandType.StoredProcedure;
Comm.commandtext = procedurename;
Da. SelectCommand = comm;
Da. Fill (DT);
}
catch (Exception ex)
{
Try
{
Connectionclose ();
}
catch (Exception e)
{
throw new Exception (e.message);
}
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
return DT;
}
}
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.