Three types of asp.net database connection classes

Source: Internet
Author: User
Tags constructor first row

Connecting to the SQL Server database

The code is as follows Copy Code

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Data.SqlClient;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;

Namespace ZZ
{
<summary>
Summary description of the classconn.
</summary>
public class conn//Database connection class
{
Public Conn ()
{
//
TODO: Add constructor logic here
//
}
Public SqlConnection connstr; Connection string
public string Getconnstr ()//Get connection string
{
String Constr;
constr=system.configuration.configurationsettings.appsettings["ConnString"];
return constr;
}
public void Open ()//Open Database
{
String Constr;
Constr=getconnstr ();
Connstr=new SqlConnection (Constr);
ConnStr. Open ();
}
public void Close ()//Shutdown database
{
ConnStr. Dispose ();
ConnStr. Close ();
}
public void Execsql (String sql)//Execute SQL statement
{
Open ();
SqlCommand cmd=new SqlCommand (SQL,CONNSTR);
Cmd. ExecuteNonQuery ();
Close ();
}
Public DataSet DataSet (String sql)//Return DataSet object
{
Open ();
SqlDataAdapter Rs=new SqlDataAdapter (SQL,CONNSTR);
DataSet ds=new DataSet ();
Rs. Fill (DS);
return DS;
}
Public DataView DataView (String sql)//Return DataView Object
{
DataSet ds=new DataSet ();
Ds=dataset (SQL);
DataView dv=new DataView (ds. Tables[0]);
return DV;
}
Public SqlDataReader DataReader (String sql)//Return DataReader Object
{
Open ();
SqlCommand cmd=new SqlCommand (SQL,CONNSTR);
SqlDataReader Dr=cmd. ExecuteReader ();
Return Dr;
}
}
}


2. Connecting to the OLE DB database

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Data.OleDb;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;

Namespace Xyl
{
<summary>
Summary description of the classconn.
</summary>
public class Classconn
{
Public Classconn ()
{
//
TODO: Add constructor logic here
//
}
Public OleDbConnection connstr;
public string Getconnstr ()
{
String Constr;
Constr= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +httpruntime.appdomainapppath+ system.configuration.configurationsettings.appsettings["ConnString"];
return constr;
}
public void Opendb ()
{
String Constr;
Constr=getconnstr ();
Connstr=new OleDbConnection (CONSTR);
ConnStr. Open ();
}
public void Closedb ()
{
ConnStr. Dispose ();
ConnStr. Close ();
}
public void Execsql (String sql)
{
Opendb ();
OleDbCommand cmd=new OleDbCommand (SQL,CONNSTR);
Cmd. ExecuteNonQuery ();
Closedb ();
}
Public DataSet Datasets (String sql)
{
Opendb ();
OleDbDataAdapter rs=new OleDbDataAdapter (SQL,CONNSTR);
DataSet ds=new DataSet ();
Rs. Fill (DS);
return DS;
}
Public DataView DataViews (String sql)
{
DataSet ds=new DataSet ();
Ds=datasets (SQL);
DataView dv=new DataView (ds. Tables[0]);
return DV;
}
Public OleDbDataReader MyDataReader (String sql)
{
Opendb ();
OleDbCommand mycom=new OleDbCommand (SQL,CONNSTR);
OleDbDataReader Myreader=mycom.executereader ();
return myreader;
}
}
}


Example Two

The code is as follows Copy Code

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)
{
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)
{
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)
{
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)
{
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)
{
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)
{
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)
{
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)
{
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)
{
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)
{
throw new Exception (ex. message);
}
Finally
{
Connectionclose ();
}
return DT;
}
}


Example Three

The code is as follows Copy Code

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Data.SqlClient;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;

Namespace ZZ
{
<summary>
Summary description of the classconn.
</summary>
public class conn//Database connection class
{
Public Conn ()
{
//
TODO: Add constructor logic here
//
}
Public SqlConnection connstr; Connection string
public string Getconnstr ()//Get connection string
{
String Constr;
constr=system.configuration.configurationsettings.appsettings["ConnString"];
return constr;
}
public void Open ()//Open Database
{
String Constr;
Constr=getconnstr ();
Connstr=new SqlConnection (Constr);
ConnStr. Open ();
}
public void Close ()//Shutdown database
{
ConnStr. Dispose ();
ConnStr. Close ();
}
public void Execsql (String sql)//Execute SQL statement
{
Open ();
SqlCommand cmd=new SqlCommand (SQL,CONNSTR);
Cmd. ExecuteNonQuery ();
Close ();
}
Public DataSet DataSet (String sql)//Return DataSet object
{
Open ();
SqlDataAdapter Rs=new SqlDataAdapter (SQL,CONNSTR);
DataSet ds=new DataSet ();
Rs. Fill (DS);
return DS;
}
Public DataView DataView (String sql)//Return DataView Object
{
DataSet ds=new DataSet ();
Ds=dataset (SQL);
DataView dv=new DataView (ds. Tables[0]);
return DV;
}
Public SqlDataReader DataReader (String sql)//Return DataReader Object
{
Open ();
SqlCommand cmd=new SqlCommand (SQL,CONNSTR);
SqlDataReader Dr=cmd. ExecuteReader ();
Return Dr;
}
}
}

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.