data reader question

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

Can somebody please tell me if I am going about this task the right way?
I have a recordset that I want to read and then perform a test and an
update on each record in the recordset. Should I use a datareader to
read the data and then a foreach loop to do my test and update? And if
so, what would the syntax of my foreach loop be?

string strGetCartDetails = "SELECT S.PRODUCTID AS 'Product ID', ";
strGetCartDetails += "'£5.00' AS 'Unit Price (GBP)', S.SIPDOMAIN
AS 'SIP Domain', ";
strGetCartDetails += "S.QUANTITY as 'Quantity', C.SIPACCOUNT AS
'SIP Account', N.STARTRANGE, N.ENDRANGE ";
strGetCartDetails += "FROM NUMBER_RANGES N INNER JOIN ";
strGetCartDetails += "SHOPPING_CART S ON ";
strGetCartDetails += "S.PRODUCTID = N.RECORDNUM ";
strGetCartDetails += "INNER JOIN SHOPPING_CART_SIP C ON ";
strGetCartDetails += "C.SCRECORDID = S.SCRECORDID ";
strGetCartDetails += "WHERE S.CARTID = '" + strCartID + "' ";
strGetCartDetails += "ORDER BY N.RECORDNUM";

SqlConnection objConnectionGetCartDetails = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTest"]);
SqlDataReader objDataReaderGetCartDetails = null;
SqlCommand objCommandGetCartDetails = new
SqlCommand(strGetCartDetails, objConnectionGetCartDetails);

objConnectionGetCartDetails.Open();
objDataReaderGetCartDetails =
objCommandGetCartDetails.ExecuteReader();

if (objDataReaderGetCartDetails.Read() == true)
{
//do foreach
}


Any help would be really appreciated.

Cheers,

Mike



*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
That's one way to go about it. You would want your loop to look like this:

while (myDataReader.Read())
{
}

You would need a separate connection to do the updates.
Another way is to put the data in a DataTable, and then loop through the
rows and run your update.
 
Back
Top