// generic method to fetch data from SQL Server into a reader
SqlDataReader fetchReader ( string qtext ) {
// connect to data source
SqlConnection sqlConn = new SqlConnection (
getConnection ( "aspnet" ) );
// initialize command object with qtext
SqlCommand sqlCmd = new SqlCommand ( qtext.Contains ( ";" ) ?
qtext.Substring ( 0, qtext.IndexOf ( ";" ) ) : qtext, sqlConn );
// open connection
sqlConn.Open ( );
// return datareader
return sqlCmd.ExecuteReader ( CommandBehavior.CloseConnection );
}
// generic method to fetch data from SQL Server into a dataset
DataSet fetchData ( string qtext ) {
// connect to data source
SqlConnection sqlConn = new SqlConnection (
getConnection ( "aspnet" ) );
// initialize dataadapter with qtext
SqlDataAdapter myAdapter = new SqlDataAdapter ( qtext.Contains ( ";" ) ?
qtext.Substring ( 0, qtext.IndexOf ( ";" ) ) : qtext, sqlConn );
// initalize and fill dataset with qtext results
DataSet myData = new DataSet ( );
myAdapter.Fill ( myData );
// return dataset
return myData;
}
// generic method to fetch scalar value from SQL Server
object fetchScalar ( string qtext ) {
// connect to data source
SqlConnection sqlConn = new SqlConnection (
getConnection ( "aspnet" ) );
// initialize command object with qtext
SqlCommand sqlCmd = new SqlCommand ( qtext.Contains ( ";" ) ?
qtext.Substring ( 0, qtext.IndexOf ( ";" ) ) : qtext, sqlConn );
// open connection
sqlConn.Open ( );
// get scalar
object scalar = sqlCmd.ExecuteScalar ( );
// close connection
sqlConn.Close ( );
// return scalar
return scalar;
}
static string getConnection ( string name ) {
string returnValue = null;
// look for the name in the connectionStrings section.
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings [ name ];
// if found, return the connection string.
if ( settings != null ) returnValue = settings.ConnectionString;
return returnValue;
}