Commandbehavior. closeconnection is used to ensure that the connection is also disabled when datareader is disabled.
Public static sqldatareader (.....)
{
Sqlcommand cmd = new sqlcommand ();
Sqlconnection conn = new sqlconnection (connectionstring );
// We use a try/catch here because if the method throws an exception we want
// Close the connection throw code, because no datareader will exist, hence
// Commandbehaviour. closeconnection will not work
Try
{
Preparecommand (CMD, Conn, null, struct type, plain text, commandparameters );
Sqldatareader SDR = cmd. executereader (commandbehavior. closeconnection );
Cmd. Parameters. Clear ();
Return SDR;
}
Catch
{
Conn. Close ();
Throw;
}
}
The using
Using (sqldatareader SDR = sqlhelper. executereader (sqlhelper. sqlconnstring, commandtype. Text, SQL _search_byid, adminparms ))
{
While (SDR. Read ())
{
Admin = new admininfo (SDR. getint32 (0), SDR. getstring (1), SDR. getstring (2 ));
}
}
Or
Sqldatareader DR = sqlhelper. executereader (sqlhelper. connectionstringprofile, commandtype. Text, sqlselect. tostring (), parms );
While (dr. Read ()){
Usernames. Add (dr. getstring (0 ));
}
Dr. Close ();
// Make sure that the status is disabled after use.