2
Answers

how to use two queries one datagrid

Photo of Demir Acar

Demir Acar

10y
1.3k
1
I have 2 server and 2 database ,  how to query 2 databases that localated different place and collect the result one datagrid .

 

for (int ii = 0; i < gridView1.RowCount; i++)

                {

                    SqlConnection conn = new SqlConnection(@"Data Source='" + tb6 + "';Initial Catalog='" + tb7 + "';user='" + tb8 + "';pwd='" + tb9 + "'");

                    SqlConnection conn = new SqlConnection(@"Data Source='" + tb1 + "';Initial Catalog='" + tb2 + "';user='" + tb3 + "';pwd='" + tb4 + "'");

                    SqlCommand cmd = new SqlCommand("SELECT ID AS REF ARACTIPI AS TIP,ARACADI AS ADI,MARKA AS MARKA FROM ARACBILGILER", conn);

                    DataTable tablo = new DataTable();

                    conn.Open();

                    conn2.Open();

                    SqlDataReader oku = cmd.ExecuteReader();

                    tablo.Load(oku);

                    SqlCommand cmd2 = new SqlCommand("SELECT DURUMU AS ARAC_DURUMU FROM ARACDB WHERE ID='" + gridView1.GetDataRow(i)["REF"] + "'", conn2);

                    SqlDataReader oku2 = cmd2.ExecuteReader();

                    tablo.Load(oku2);

                    gridControl1.DataSource = tablo;

conn.Close();

conn2.Close();

}

}

}

Answers (2)

0
Photo of Demir Acar
NA 98 26.3k 10y
I am sorry its only spelling mistake , My string is ;

 SqlConnection conn = new SqlConnection(@"Data Source='" + tb6 + "';Initial Catalog='" + tb7 + "';user='" + tb8 + "';pwd='" + tb9 + "'");

                    SqlConnection conn2 = new SqlConnection(@"Data Source='" + tb1 + "';Initial Catalog='" + tb2 + "';user='" + tb3 + "';pwd='" + tb4 + "'");


and use windows application therefore dont need to webconfig , in this case how can u help me .

0
Photo of Anubhav Chaudhary
NA 41.8k 20.1m 10y
Hey Birol,


I am sure your code will be providing error to you, that's because you are declaring same connection name two times i.e. 

SqlConnection conn = new SqlConnection(@"Data Source='" + tb6 + "';Initial Catalog='" + tb7 + "';user='" + tb8 + "';pwd='" + tb9 + "'");

 SqlConnection conn = new SqlConnection(@"Data Source='" + tb1 + "';Initial Catalog='" + tb2 + "';user='" + tb3 + "';pwd='" + tb4 + "'");


Change these name, secondly if you are using web.config then also provide two different names for connection and use them separately.

Now coming to your second problem "How to integrate them, so for this problem firstly open a connection and do your work in it after doing your work close it. After this again open the connection and again do your work, now you will be having work in two different connections, so no problem just use the name of your variables outside the connections and play with them as you want

Thanks