473,382 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Is SqlDataReader Thread Safe?

I am using SQLDataReader to do a rather simple select statement, and then
running an update in the while loop for the Read method.

When I place the SQLDataReader in a thread delegate or not, it always pulls
duplicates for example my database contains the following:

TestID
1
2
3

And I output my values

string strTestID = "";

while(Connection1.Read())
{
strTest = Connection1[0].ToString();
}

It will output

1
1
2
2
3
3

If I take this out of a thread it will output like it should
1
2
3

Any ideas on what might be going on?

Nov 15 '05 #1
3 4281
Matt,

While the data reader is not thread safe (it says as much in the
documentation), I think that this stems from the connection, and not the
reader itself. You should be marshaling the call across threads, or
establishing the connection on the thread that will access it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matt" <mi*****@nospam.yahoo.com> wrote in message
news:OA**************@tk2msftngp13.phx.gbl...
I am using SQLDataReader to do a rather simple select statement, and then
running an update in the while loop for the Read method.

When I place the SQLDataReader in a thread delegate or not, it always pulls duplicates for example my database contains the following:

TestID
1
2
3

And I output my values

string strTestID = "";

while(Connection1.Read())
{
strTest = Connection1[0].ToString();
}

It will output

1
1
2
2
3
3

If I take this out of a thread it will output like it should
1
2
3

Any ideas on what might be going on?

Nov 15 '05 #2
Nicholas,

Thanks, ya actually I do establish the connection on the thread that will
access it.

Below is a general idea of what I am doing, Console.WriteLine actually will
duplicate each selected record.

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;da tabase=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())
}
}

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Oq**************@TK2MSFTNGP12.phx.gbl...
Matt,

While the data reader is not thread safe (it says as much in the
documentation), I think that this stems from the connection, and not the
reader itself. You should be marshaling the call across threads, or
establishing the connection on the thread that will access it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matt" <mi*****@nospam.yahoo.com> wrote in message
news:OA**************@tk2msftngp13.phx.gbl...
I am using SQLDataReader to do a rather simple select statement, and then running an update in the while loop for the Read method.

When I place the SQLDataReader in a thread delegate or not, it always

pulls
duplicates for example my database contains the following:

TestID
1
2
3

And I output my values

string strTestID = "";

while(Connection1.Read())
{
strTest = Connection1[0].ToString();
}

It will output

1
1
2
2
3
3

If I take this out of a thread it will output like it should
1
2
3

Any ideas on what might be going on?


Nov 15 '05 #3
nicholas,

I actually I have to do this in my Connection1.Read() while loop to get it
to work properly..

string strTestID = "";

while(Connection1.Read())
{
if(strTestID != Connection1[0].ToString()))
{
Console.WriteLine (Connection1[0].ToString())

strTestID = Connection1[0].ToString());
}

}

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Oq**************@TK2MSFTNGP12.phx.gbl...
Matt,

While the data reader is not thread safe (it says as much in the
documentation), I think that this stems from the connection, and not the
reader itself. You should be marshaling the call across threads, or
establishing the connection on the thread that will access it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matt" <mi*****@nospam.yahoo.com> wrote in message
news:OA**************@tk2msftngp13.phx.gbl...
I am using SQLDataReader to do a rather simple select statement, and then running an update in the while loop for the Read method.

When I place the SQLDataReader in a thread delegate or not, it always

pulls
duplicates for example my database contains the following:

TestID
1
2
3

And I output my values

string strTestID = "";

while(Connection1.Read())
{
strTest = Connection1[0].ToString();
}

It will output

1
1
2
2
3
3

If I take this out of a thread it will output like it should
1
2
3

Any ideas on what might be going on?


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
2
by: Matthias S. | last post by:
Hi, I've written a simple app which should just fetch some data from a database and render the results into a ListView. In order to not freeze the GUI, I'm using a BackgroundWorker. The...
3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
1
by: jecheney | last post by:
Hi, Im currently using the following code for reading/writing to a network socket. private StreamReader clientStreamReader; private StreamWriter clientStreamWriter; .... TcpClient tcpClient...
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
12
by: Peter K | last post by:
Say I have this class public class Simple { private string name; public string Name { get { return name; } set { name = value; }
44
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it...
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.