Tuesday, May 13, 2008

Multiple Queries Execution

When we need to select values from two different tables in to two datasets, then normally we will be making a call to the database two times. But we can do this from a single query. Now we will see the syntax to do multiple queries inside a single SqlCommand for SQL Server. What happen is that one call will be made to the database server - and one call back (containing ALL results).

The nice thing about ADO.NET is that is stuffs everything into our dataset tables collection.




The code for this:



string sConn = "Data Source=(local);Network Library=DBMSSOCN;Initial Catalog=Northwind;User ID=test;Password=test;";
string sSQL = "select * from products;select * from categories";
DataSet dd = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(sConn,
CommandType.Text, sSQL);
dataGridView1.DataSource = dd.Tables[0];
dataGridView2.DataSource = dd.Tables[1];

As you can see the products are put into tables[0] and categories into tables[1].


No comments:

Post a Comment