using (DataReader) inside loop
-
As far as performance issues are concerned I have a question .... I have to make multiple selects based on date inside of a loop. Is it wise to use the USING statement for the datareader inside of the loop or does something need to be done outside the loop. Here is my current code.
using (SQLCommand selectAnnv = new SQLCommand(_database)) { //create paramater date for variable select SQLParameter paramDate = new SQLParameter("@DATE"); // create parement query @DATE is variable selectAnnv.CommandText = "SELECT id FROM " + TMEMBERS + " WHERE registered = '(@DATE)'"; selectAnnv.Parameters.Add(paramDate); // loop through each day missed to current day for (DateTime anniversary = lastHandled.AddDays(1); anniversary <= now; anniversary = anniversary.AddDays(1)) { paramDate.Value = GStatic.GetISO8601(anniversary); _**//add ids to list // Data reader intialized outside loop using (SQLDataReader readerAnnv = selectAnnv.ExecuteReader()) { while (readerAnnv.Read()) ids.Add(readerAnnv.GetInt32(0)); }**_ } }
Should the bolded/italic part be decalered outside of the loop? -
As far as performance issues are concerned I have a question .... I have to make multiple selects based on date inside of a loop. Is it wise to use the USING statement for the datareader inside of the loop or does something need to be done outside the loop. Here is my current code.
using (SQLCommand selectAnnv = new SQLCommand(_database)) { //create paramater date for variable select SQLParameter paramDate = new SQLParameter("@DATE"); // create parement query @DATE is variable selectAnnv.CommandText = "SELECT id FROM " + TMEMBERS + " WHERE registered = '(@DATE)'"; selectAnnv.Parameters.Add(paramDate); // loop through each day missed to current day for (DateTime anniversary = lastHandled.AddDays(1); anniversary <= now; anniversary = anniversary.AddDays(1)) { paramDate.Value = GStatic.GetISO8601(anniversary); _**//add ids to list // Data reader intialized outside loop using (SQLDataReader readerAnnv = selectAnnv.ExecuteReader()) { while (readerAnnv.Read()) ids.Add(readerAnnv.GetInt32(0)); }**_ } }
Should the bolded/italic part be decalered outside of the loop? -
As far as performance issues are concerned I have a question .... I have to make multiple selects based on date inside of a loop. Is it wise to use the USING statement for the datareader inside of the loop or does something need to be done outside the loop. Here is my current code.
using (SQLCommand selectAnnv = new SQLCommand(_database)) { //create paramater date for variable select SQLParameter paramDate = new SQLParameter("@DATE"); // create parement query @DATE is variable selectAnnv.CommandText = "SELECT id FROM " + TMEMBERS + " WHERE registered = '(@DATE)'"; selectAnnv.Parameters.Add(paramDate); // loop through each day missed to current day for (DateTime anniversary = lastHandled.AddDays(1); anniversary <= now; anniversary = anniversary.AddDays(1)) { paramDate.Value = GStatic.GetISO8601(anniversary); _**//add ids to list // Data reader intialized outside loop using (SQLDataReader readerAnnv = selectAnnv.ExecuteReader()) { while (readerAnnv.Read()) ids.Add(readerAnnv.GetInt32(0)); }**_ } }
Should the bolded/italic part be decalered outside of the loop?Why not just use a date range in your sql statement?
Logifusion[^] If not entertaining, write your Congressman.
-
Why not just use a date range in your sql statement?
Logifusion[^] If not entertaining, write your Congressman.
-
Could you clarify that statement for me? You're using a SQLDataReader, so you're talking to a SQL Server.
Logifusion[^] If not entertaining, write your Congressman.
-
Could you clarify that statement for me? You're using a SQLDataReader, so you're talking to a SQL Server.
Logifusion[^] If not entertaining, write your Congressman.