asp.net database Connection and DataTable class _ Practical skills

Source: Internet
Author: User
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;
<summary>
Summary description of Sqlconnections
</summary>
Namespace System.mysqlconnection
{
public class Sqlconnections
{
The current connection is the first few.
static int count = 0;
Maximum Connection Amount
static int maxcount = 30;
Static sqlconnection[] Sqlconns = new Sqlconnection[maxcount];
Create your own
static Sqlconnections myconn = new Sqlconnections ();
Class
Private Sqlconnections ()
{
for (int i = 0; i < Maxcount; i++)
{
Sqlconns[i] = new SqlConnection (system.configuration.configurationsettings.appsettings["ADOStr"). ToString ());
}
}
According to the criteria to get different tables, and fields.
public static DataTable getdatatable (String sqltest, String tablename)
{
Try
{
DataSet myds = new DataSet ();
SqlDataAdapter myadapater = new SqlDataAdapter (Sqltest, Sqlconnections.getsqlconnection ());
Myadapater.fill (myDS, TableName);
return Myds.tables[tablename];
}
catch (SqlException Sqle)
{
return null;
}
}
Getting a database connection
public static SqlConnection Getsqlconnection ()
{
Try
{
Try
{
Sqlconns[count]. Close ();
}
catch (Exception EeX)
{
}
return Sqlconns[count];
}
Finally
{
Current number from Add ...
if (count = = (maxCount-1))
{
Count = 0;
}
Else
{
count++;
}
}
}
Returns a parameter
public static string Getnumonefield (String sqltxt)
{
SqlConnection sqlconntemp = Sqlconnections.getsqlconnection ();
Try
{
SqlCommand sqlcommt = new SqlCommand (Sqltxt, sqlconntemp);
Sqlconntemp.open ();
Return Sqlcommt.executescalar (). ToString ();
}
catch (Exception Sqle)
{
Return "No";
}
Finally
{
Sqlconntemp.close ();
}
}
Returns a parameter
public static string Getnumonefield (SqlConnection sqlconntemp,sqltransaction Tempsqltran, String sqltxt)
{
Try
{
SqlCommand sqlcommt = new SqlCommand (Sqltxt, sqlconntemp);
Sqlcommt.transaction = Tempsqltran;
Return Sqlcommt.executescalar (). ToString ();
}
catch (Exception Sqle)
{
Return "No";
}
Finally
{
Sqlconntemp.close ();
}
}
<summary>
Do, modify, delete,
</summary>
<param name= "sqltxt" > Executed SQL statements </param>
<returns> How many rows are affected by the results returned </returns>
public static int Updateordelete (String sqltxt)
{
SqlConnection sqlconntemp = Sqlconnections.getsqlconnection ();
Try
{
SqlCommand sqlcommt = new SqlCommand (Sqltxt, sqlconntemp);
Sqlconntemp.open ();
return Sqlcommt.executenonquery ();
}
catch (SqlException Sqle)
{
return 0;
}
Finally
{
Sqlconntemp.close ();
}
}
public static int Updateordelete (CommandType _commandtype, sqlparameter[] _sqlparas, String sqltxt)
{
SqlConnection sqlconntemp = Sqlconnections.getsqlconnection ();
Try
{
SqlCommand sqlcommt = new SqlCommand (Sqltxt, sqlconntemp);
Sqlcommt.commandtype = _commandtype;
SqlCommT.Parameters.Clear ();
for (int a = 0; a < _sqlparas.length; a++)
{
SQLCOMMT.PARAMETERS.ADD (_sqlparas[a]);
}
Sqlconntemp.open ();
return Sqlcommt.executenonquery ();
}
catch (SqlException Sqle)
{
return 0;
}
Finally
{
Sqlconntemp.close ();
}
}
<summary>
With the increase of the transaction, delete, change the Data execution method (Warning: This method must manually switch data, and there are manual commit transactions.) Otherwise the data cannot be updated)
</summary>
<param name= "_sqlconn" > Database connection Objects </param>
<param name= "_sqltran" > Transaction Object </param>
<param name= "sqltxt" > Executed SQL statements </param>
<returns></returns>
public static int Updateordelete (SqlConnection _sqlconn,sqltransaction _sqltran, String sqltxt)
{
SqlConnection sqlconntemp = _sqlconn;
Try
{
SqlCommand sqlcommt = new SqlCommand (Sqltxt, sqlconntemp);
Sqlcommt.transaction = _sqltran;
Sqlconntemp.open ();
return Sqlcommt.executenonquery ();
}
catch (SqlException Sqle)
{
return 0;
}
Finally
{
Sqlconntemp.close ();
}
}
public static int Updateordelete (SqlConnection _sqlconn, SqlTransaction _sqltran, CommandType _commandtype, SqlParameter [] _sqlparas, String sqltxt)
{
SqlConnection sqlconntemp = _sqlconn;
Try
{
SqlCommand sqlcommt = new SqlCommand (Sqltxt, sqlconntemp);
Sqlcommt.transaction = _sqltran;
Sqlcommt.commandtype = _commandtype;
SqlCommT.Parameters.Clear ();
for (int a = 0; a < _sqlparas.length; a++)
{
SQLCOMMT.PARAMETERS.ADD (_sqlparas[a]);
}
return Sqlcommt.executenonquery ();
}
catch (SqlException Sqle)
{
return 0;
}
Finally
{
Sqlconntemp.close ();
}
}
<summary>
There are stored queries
</summary>
<param name= "storedprocedurename" > Stored procedure name </param>
<param name= "Sqlparas" > All Parameters </param>
<param name= "tablename" > table name </param>
<returns>datatable result set </returns>
public static DataTable getdatatable (String storedprocedurename, SqlParameter [] sqlparas,string tablename)
{
Try
{
DataSet myds = new DataSet ();
SqlDataAdapter myadapater = new SqlDataAdapter (StoredProcedureName, Sqlconnections.getsqlconnection ());
MyAdapater.SelectCommand.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < sqlparas.length; i++)
{
if (MyAdapater.SelectCommand.Parameters.Contains (Sqlparas[i]))
MyAdapater.SelectCommand.Parameters.RemoveAt (Sqlparas[i]. ParameterName);
MYADAPATER.SELECTCOMMAND.PARAMETERS.ADD (Sqlparas[i]);
}
Myadapater.fill (myDS, TableName);
MyAdapater.SelectCommand.Parameters.Clear ();
Myadapater.dispose ();
return Myds.tables[tablename];
}
catch (SqlException Sqle)
{
return null;
}
}
}
}

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.