SqlHelper tool class,

Source: Internet
Author: User

SqlHelper tool class,

Public class SqlHlper
{
Public static readonly string constr = ConfigurationManager. ConnectionStrings ["constr"]. ConnectionString;
// Execute add, delete, and modify operations
Public static object ExecuteNonQuery (string SQL, params SqlParameter [] pms) // SqlParameter must call Data. SqlClient
{
Using (SqlConnection con = new SqlConnection (constr ))
{
Using (SqlCommand cmd = new SqlCommand (SQL, con ))
{
If (pms! = Null)
{
Cmd. Parameters. AddRange (pms );
}
Con. Open ();
Return cmd. ExecuteNonQuery ();

}
}
}
// Obtain a single data
Public static object ExecteScalar (string SQL, params SqlParameter [] pms)
{
Object obj = null;
Using (SqlConnection conn = new SqlConnection (constr ))
{
SqlCommand cmd = new SqlCommand (SQL, conn );
Conn. Open ();
If (pms! = Null)
{
Cmd. Parameters. AddRange (pms );
}
Obj = cmd. ExecuteScalar ();
}
Return obj;
}
// Obtain multiple data
Public static SqlDataReader GetDataReader (string SQL, params SqlParameter [] sps)
{
SqlConnection conn = new SqlConnection (constr );
Using (SqlCommand cmd = new SqlCommand (SQL, conn ))
{
If (sps! = Null)
{
Cmd. Parameters. AddRange (sps );
}
Conn. Open ();
Return cmd. ExecuteReader (CommandBehavior. CloseConnection );
}
}
// Query multiple data entries
Public static DataTable GetDataTable (string SQL, params SqlParameter [] sps)
{
DataTable dt = new DataTable ();
Using (SqlDataAdapter da = new SqlDataAdapter (SQL, constr ))
{
If (sps! = Null)
{
Da. SelectCommand. Parameters. AddRange (sps );
}
Da. Fill (dt );
}
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.