سؤال

following code works with a single DataSet however when I try to do it with a DataSet array SqlDataAdapter.Fill() does not work. Basically what I am trying to do is connecting 6 different databases and run the same sql query on them and collect all the data together and show the whole data from 6 dbs in single dataGridView. How to do it? Thanks,

DataSet[] mySet = new DataSet[6];   
DataSet finSet = new DataSet();               

for (int j = 0; j <= 5; j++)

                myConnection.Open();

                for (int i = 0; i <= specRowCount - 2; i++)
                {

                    cleanDesc = dataGridView2.Rows[removalPointer].Cells[1].Value.ToString().Replace("'", "''").Trim();
                    classname = dataGridView2.Rows[removalPointer].Cells[0].Value.ToString().Trim();

                    str = "use " + myDatabases[j] + " SELECT top 1 x, y, z, t, h, f, d, " +
                    "s, d, c, s, a, d, f, g, " +
                    "s, f, a, dFROM tttt where c=1 and a=1 and " +
                    "d='" + cleanDesc + "' and d= '" + d+ "'";

                    myAdapter = new SqlDataAdapter(str, myConnection);

                    myAdapter.Fill(mySet[j], "tttt");


                    if (countRows != mySet[j].Tables["tttt"].Rows.Count)
                    {

                        mySb.AppendLine(dataGridView2.Rows[removalPointer].Cells[1].Value.ToString());


                        dataGridView2.Rows.Remove(dataGridView2.Rows[removalPointer]);


                    }
                    else
                    {

                        removalPointer++;

                    }



                    countRows = mySet[j].Tables["tttt"].Rows.Count;

                    finSet.Merge(setArray[j]);

                    dataGridView1.DataSource = finSet.Tables["tttt"];


                }
                myConnection.Close();

                MessageBox.Show("Con closed!");


            }
هل كانت مفيدة؟

المحلول

Just based on the code you have shown us, it looks like the problem is that you fill mySet[j] from the Data Adapter but then merge setArray[j] into finSet. Thus the data from the Data Adapter never makes it into finSet.

Another thing: I'm pretty sure that this code:

DataSet[] mySet = new DataSet[6]; 

... will create an array of DataSets, but the array will contain nulls for every element. That's because you've created an array, but you haven't created DataSet objects to fill it.

So this code will throw an error, since you're attempting to fill a Null:

myAdapter.Fill(mySet[j], "tttt");

Try something like this:

mySet[j] = new DataSet();
myAdapter.Fill(mySet[j], "tttt");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top