Multithreading information
I am trying use Thread but i have some problem (I am beginner at the threading)
my codes like that. I have a dataset in form and I want to get some tablequery from DataBase and crete adapter and fiil the dataset in form
Dataset mydataset=new DataSet();
private void Form_Load(object sender,eventargs e)
{
SqlConnection con=new SqlConnection("constring");
SqlDataAdapter adap=new SqlDataAdapter("select * from Tables");
DataTable dt=new DataTable();
adap.Fill(dt);
foreach(DataRow dr in dt.Rows)
{
ThreadStarter mystarter=delegate{ CreateTable(dr); };
Thread mythread=new Thread();
mythread.Start();
}
}
void CreateTable(DataRow dr)
{
SqlDataAdapter adap2=new SqlDataAdapter(dr["Query"].toString(),con);
ada2.Fiil(myDataSet);
}
when I use this cod开发者_如何学Ce .I get a error like "There is already SqlDataReader for closed first" What can I do?
I think you're going to need to explain what you're trying to do here. It looks like you're opening a list of tables, and then trying to load all the data from all those tables, in parallel, but all into the same single object... That doesn't make a lot of sense.
For what it's worth, the error seems to be that you're creating multiple data adapters that are going to use the same database connection at the same time - you can't do that. If you really wanted to load all this data in parallel, you'd need to make multiple connections to the database.
A SqlConnection
connection only support one command (and one reader) at a time. It cannot be concurrently accessed by several threads. So you need to create a separae SqlConnection
instance in each thread.
精彩评论