Encapsulation of Database Operations

Source: Internet
Author: User
Public   Static   String Constring =   @" Server = localhost; Integrated Security = true; database = northwind " ;

1. Execute the SQL statement and return the affected number of rows.

Code
String Constring =   @" Server = localhost; Integrated Security = true; database = northwind " ;
// Execute the SQL statement and return the affected number of rows.
Public   Int Executesql ( String Strsql)
{
Sqlconnection thisconnection =   New Sqlconnection (constring );
Sqlcommand thiscommand =   New Sqlcommand (strsql, thisconnection );
Thisconnection. open ();
Int Rows = Thiscommand. executenonquery ();
Thisconnection. Close ();
Return Rows;
}

Call method: StringStrsql=@"Select * from MERs";
IntRows=Executesql (strsql );

I don't know why. After testing, the returned rows is-1. No reason is found.

2. Execute the query statement and return Dataset Code
// Execute the SQL statement and return Dataset
Public   Static Dataset query ( String Strsql)
{
Sqlconnection thisconnection =   New Sqlconnection (constring );
Dataset thisdataset =   New Dataset ();
Thisconnection. open ();
Sqldataadapter thisadapter =   New Sqldataadapter (strsql, constring );
Thisadapter. Fill (thisdataset, " DS " );
Thisconnection. Close ();
Return Thisdataset;
}

Call method:

String Strsql =   @" Select * from MERs " ;
Dataset DS = Utility. Query (strsql );

3. Execute a statement to calculate the query result and return the query result (object) Code
// Execute a calculation Query Result Statement and return the query result (object)
Public   Static   Object Getsingle ( String Strsql)
{
Sqlconnection thisconnection =   New Sqlconnection (constring );
Sqlcommand thiscommand =   New Sqlcommand (strsql, thisconnection );
Thisconnection. open ();
Object OBJ = Thiscommand. executescalar ();
If (Object. Equals (OBJ, Null )) | (Object. Equals (OBJ, system. dbnull. Value )))
{
Return   Null ;
}
Else
{
Return OBJ;
}
}

Call method:

String Strsql =   @" Select * from MERs " ;
String Num = Utility. getsingle (strsql). tostring ();

5. Execute SQL statements with parameters and return the number of affected records
..............

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.