Asp.net (C #) is a simple data pipeline class.

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Using system. configuration;
Using system. Data;
Using system. Data. sqlclient;

Public static class dbtool
{

/// <Summary>
/// Establish a connection
/// </Summary>
/// <Returns> sqlconnection </returns>
Public static sqlconnection getconnection ()
{
String connstr = configurationmanager. connectionstrings ["conn"]. connectionstring; // obtain the connection string from the configuration file

Sqlconnection conn = new sqlconnection (connstr );
Return conn;
}

/// <Summary>
/// Used for select statements
/// </Summary>
/// <Param name = "SQL"> SQL statement </param>
/// <Param name = "Parameters"> query parameters </param>
/// <Returns> datatable </returns>
Public static datatable getdatatable (string SQL, Params sqlparameter [] parameters)
{
Datatable dt = new datatable ();
Sqlconnection conn = getconnection ();

Sqlcommand cmd = new sqlcommand (SQL, Conn );

Cmd. Parameters. addrange (parameters );
Sqldataadapter da = new sqldataadapter (CMD );

Da. Fill (DT); // fill data with DT

Return DT;
}

// A method that can be used for non-select statements. It is mainly used for insert, delete, and update statements.
/// <Summary>
/// Used for update, insert, and delete statements
/// </Summary>
/// <Param name = "SQL"> SQL statement </param>
/// <Param name = "Parameters"> query parameters </param>
/// <Returns> int32 </returns>
Public static int executenonquery (string SQL, Params sqlparameter [] parameters)
{
Int I = 0;
Using (sqlconnection conn = getconnection ())
{
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Cmd. Parameters. addrange (parameters );

Conn. open ();

I = cmd. executenonquery ();

}
Return I;
}

/// <Summary>
/// Read the information in the data stream
/// </Summary>
/// <Param name = "SQL"> SQL statement </param>
/// <Param name = "Parameters"> query parameters </param>
/// <Returns> sqldatareader </returns>
Public static sqldatareader executereader (string SQL, Params sqlparameter [] parameters)
{
Sqlconnection conn = getconnection ();

Sqlcommand cmd = conn. createcommand ();
Cmd. commandtext = SQL;

Foreach (sqlparameter P in parameters)
{
Cmd. Parameters. Add (P );
}

Conn. open ();

Sqldatareader SDR = cmd. executereader (); // select, return result set

Return SDR;
}
}

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.