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
..............