Visual C # tutorial on Dynamically Operating SQL Server database instances (5): Method for returning a dataset datareader
As described aboveSQL statement execution method with ParametersAndHow to execute a stored procedure with Parameters, This article introducesMethod for returning the dataset datareader, It callsSqlhelper)Run the sqlhelper. executereader () method. The example is;
Sqldatareader mydr = sqlhelper. executereader (sqlhelper. connectionstringlocaltransaction, commandtype. Text, SQL, null );
The four parameters are passed:
"Sqlhelper. connectionstringlocaltransaction ,"-Call the generic data handler class, Returns the link string;
"Commandtype. Text"-indicates the type of SQL command to be executed;
"SQL"-the SQL command statement to be executed;
"Null"-the fourth parameter is null.
Protected void btnexecutereader_click (Object sender, eventargs E)
{
// Obtain the command to be executed
String SQL = "select top 5 title, classname, hints, dateantime, ArticleID, classid_a, classid, classid_c from menularticle where classid_a = 1 and headline = 1 order by hints DESC ";
Sqlcommand cmd = new sqlcommand ();
// Defines the storage range of object Resources. Once the using range ends, the resources occupied by the other party are released.
// Call the execution method. Because there is no parameter, the last item is directly set to null.
// Note: The returned result is of the sqldatareader type.
Sqldatareader mydr = sqlhelper. executereader (sqlhelper. connectionstringlocaltransaction, commandtype. Text, SQL, null );
// Assign the result to datalist as its Data Source
Datalist1.datasource = mydr;
// Bind data
Datalist1.databind ();
}
For more information, see my website.