Google

Tuesday, November 6, 2007

To retrieve fields using SQLReader

Whenever the fields needs to be retrieved from SQL Database, The smartest way is to do it when there is lot of data to retrieve is through the SQLDataReader. The code can look like this :--

private SqlConnection sConn; 
private SqlCommand slSelectSumDClosedCommand; 
slSelectSumDClosedCommand = new SqlCommand(); 
slSelectSumDClosedCommand.CommandText = "Select * from table1"; 
slSelectSumDClosedCommand.Connection = sConn; 
private SqlDataReader reader=null; 
private Hashtable sDClosedValue = new Hashtable(); 

reader = slSelectSumDClosedCommand.ExecuteReader(); 
while(reader.Read()) 
{ 
string acct = reader.GetString(0).Trim(); 
double dailyVal = reader.GetDouble(1); 
sDClosedValue.Add(acct, dailyVal); 
} 
reader.Close(); 
The above code is reading values from the table through data reader and putting it in an hashtable. You can carry out whatever function is required. Try using stored procedure everytime. Above code is just an example that is why I have used SQLCommand.CommandText.

No comments: