sqldatareader in a thread
I'm having a wierd issue where i run a query in a thread and the while(Connection.read()) duplicates my results. Anyone else have this issue? For isntance if I have TestTable with the following values:
TestID
1
2
3
It will console.writeline
1
1
2
2
3
3
Below is sample code:
private void button3_Click(object sender, System.EventArgs e)
{
ThreadStart DBThread = new ThreadStart(StartDBDelegate) ;
Thread DB = new Thread(DBThread) ;
DB.Start();
}
public void StartDBDelegate)()
{
Invoke(new UpdateLoadTableDelegate(RunUpdateNow));
}
public void RunUpdateNow()
{
string source =
"server=testserver;uid=testuid;pwd=testpassword;database=TestDB";
string selectconn1 = "SELECT TestID as dbTestID FROM TestTable"
SqlConnection conn1 = new SqlConnection(source);
conn1.Open();
SqlCommand executeconn1 = new SqlCommand(selectconn1,conn1);
SqlDataReader Connection1 = executeconn1.ExecuteReader();
while(Connection1.Read())
{
Console.WriteLine (Connection1[0].ToString())
}
}
I can reduce this menace by doing the following to the while loop, although I shouldnt have to do this...this is really bugging me. It doesnt do this outside of the thread.
string strTestID = "";
while(Connection1.Read())
{
if(strTestID != Connection1[0].ToString()))
{
Console.WriteLine (Connection1[0].ToString())
strTestID = Connection1[0].ToString());
}
}