C# .NET - Can we have multiple queries in sqldataadapter

Asked By shweta on 17-May-10 01:55 AM
Can we have multiple queries in sqldataadapter
Santhosh N replied to shweta on 17-May-10 02:00 AM
you could actually have one single resultset in one single call and once yo fetch you can have different query but not simultaneoulsly..
Sagar P replied to shweta on 17-May-10 02:12 AM
Yes we can have multiple quries in sqldataadapter.

Filling a DataSet with multiple tables can be done by sending multiple requests to the database, or in a faster way: Multiple SELECT statements can be sent to the database server in a single request. The problem here is that the tables generated from the queries have automatic names Table and Table1. However, the generated table names can be mapped to names that should be used in the DataSet

SqlDataAdapter adapter = new SqlDataAdapter(
    "SELECT * FROM Customers; SELECT * FROM Orders", connection);
adapter.TableMappings.Add("Table", "Customer");
adapter.TableMappings.Add("Table1", "Order");

adapter.Fill(ds);

Web Star replied to shweta on 17-May-10 02:13 AM
yes u can use multiple query for fetching different resultset using sql dataadapter as follows

stored proc

Create proc spMultipleRecordset

Begin

Select * from tablname1
Select * from tablname2
Select * from tablname3

End

and than put code for fill dataset using dataadapter as follows
Create your C# code:
SqlCommand myCommand = new SqlCommand("dbo.MyStoredProcedure");
myCommand.CommandType = CommandType.StoredProcedure;

// create SqlConnection
SqlConnection myConnection = new SqlConnection("your connection string here");
myCommand.Connection = myConnection;
SqlDataAdapter da = new SqlDataAdapter(myCommand);

DataSet data = new DataSet();
da.Fill(data);

//here your 3 result set fill with same dataset ds and u will get each with using
ds.Tables[0],ds.Tables[1],ds.Tables[2],respectively

Anoop S replied to shweta on 17-May-10 02:14 AM
Ya you can have multiple sql queries in sql dataadapter like

da.TableMappings.Add("Table", "name1");
da.TableMappings.Add("Table1", "name2");
da.TableMappings.Add("Table2", "name3");

but it is considered bad practice to run multiple queries in a sqlcommand to pull data like this because it only have one sqldataadapter object, instead of that you can use JOIN if there is any relation exist between tables
Goniey N replied to shweta on 17-May-10 07:48 AM
Yes It Is Possible That In One SQLDataAdapter Multiple Queries Like :



SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Students; SELECT * FROM Subjects; SELECT * FROM Courses", myConn(Your Connection Name) );
da.TableMappings.Add("Students", "Subjects", "Courses");
DataSet ds = new DataSet();
da.Fill(ds, "Students"); //Fill All Data From Students Table
da.Fill(ds, "Subjects"); //Fill All Data From Subjects Table
da.Fill(ds, "Courses"); //Fill All Data From Courses Table
Murat replied to Goniey N on 27-Nov-10 10:12 AM
How can I fill datagridviev?