Click here to Skip to main content
15,904,297 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I am looking for some code that will read all the rows of a table called table1 every 10 second....anyone knows how to do it best...thanks
Posted

Assuming that you mean an SQL table, then just put the normal code in a Timer event.
Try:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT * FROM MyTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                ...
                }
            }
        }
    }
But it is probably a very bad idea to do it - you will cause enormous amounts of unnecessary traffic, and work for the SQL server. What are you trying to achieve, that you think this will be a good idea? There may be a better way.
 
Share this answer
 
Comments
EricThe 10-Nov-11 3:10am    
Basically I have a table from which i have to update all rows in a databound in aspx ....I have the aspx that shows data for one row using c# but dont have good idea how to put it in loop or something to read all rows of table every 10 seconds.......here is the code I have part of it

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
con.Open();
cmd = new SqlCommand("SELECT f,g,h,i,j,k FROM wa1 WHERE email='" + email.Text + "'", con);
dr = cmd.ExecuteReader();
con.Close();
}
Use timer..

private void timer1_Tick(object sender, EventArgs e)
        {
        // Your data retrieval code

          }
 
Share this answer
 
v2
Comments
EricThe 10-Nov-11 3:16am    
thanks how would you loop through each row of table???
lavikgupta 10-Nov-11 13:07pm    
dr.Read() returns false when all rows of table has been read.

so -->
while(dr.Read())
{
//read columns from data reader like dr[0],dr[1]......
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900