C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Videos


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...


Birthday Greetings
Learn Windows 7: Miracle of Mouse or simply mouse magic   Bored of double clicking or cannot double click. Go through my article below and you would be surprised to know the miracle of Mouse



Forums » .NET » .NET »

How can I fill multiple DataTables in a DataSet ?


Posted Date: 23 Jan 2007      Posted By:: vanitha    Member Level: Bronze    Member Rank: 0     Points: 2   Responses: 6



Hi,

How can i fill multiple datatables into a dataset. So that I need to display values retrieved from two different tables in a datagrid in vb.net.





Responses

Author: veeresh     Member Level: Diamond      Member Rank: 0     Date: 23/Jan/2007   Rating: 2 out of 52 out of 5     Points: 2

One way to fill a DataSet with multiple tables is to send the database multiple requests. Another way to do this is to use multiple SELECT statements in a single request.

There are a couple of problems with doing it this way:

The DataTables don't have the same name as the tables in the database, you have to set them yourself
You can't update/save the tables to the database; to do that you must use a seperate DataAdapter for each table.
Dim myAdapter as SqlDataAdapter = new SqlDataAdapter(
“SELECT * FROM Customers; SELECT * FROM Orders“, connection)

myAdapter.Fill(dsTables)
dsTables.Tables(0).TableName = “Customers“)
dsTables.Tables(1).TableName = “Orders“)

It would be so much easier if they made it so you can use the same DataAdapter to update all the tables you load into the DataSet with the DataAdapter.



Author: veeresh     Member Level: Diamond      Member Rank: 0     Date: 23/Jan/2007   Rating: 2 out of 52 out of 5     Points: 2

Populate a DataSet with multiple DataTable objects using multiple SELECT statements




using System;
using System.Data;
using System.Data.SqlClient;

class MultipleDataTables
{
public static void Main()
{
SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT TOP 2 ID, FirstName, LastName " +
"FROM Employee " +
"ORDER BY ID;" +
"SELECT ID, LastName " +
"FROM MyEmployee " +
"WHERE ID = 9;";

SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
int numberOfRows = mySqlDataAdapter.Fill(myDataSet);
Console.WriteLine("numberOfRows = " + numberOfRows);
mySqlConnection.Close();

myDataSet.Tables["Table"].TableName = "Employee";
myDataSet.Tables["Table1"].TableName = "MyEmployee";
}
}



Author: Vadivel Mohanakrishnan     Member Level: Diamond      Member Rank: 0     Date: 23/Jan/2007   Rating: 2 out of 52 out of 5     Points: 2

There are multiple ways of doing it.

1. Make use of multiple select statements within your adapter. The only pt to note is, you need to make use of semicolon (;) between two queries.

2. Easiest of the lot is to make use of Stored procedure. Within a stored procedure write multiple select statements. Recordset returned by each query would get filled within one datatable in the given Dataset.

Best Regards
Vadivel

MVP SQL Server
http://vadivel.blogspot.com



Author: Venkatesh Murali     Member Level: Gold      Member Rank: 0     Date: 19/Dec/2007   Rating: 2 out of 52 out of 5     Points: 2

Populate a DataSet with multiple DataTable objects using multiple SELECT statements


Author: Venkatesh Murali     Member Level: Gold      Member Rank: 0     Date: 19/Dec/2007   Rating: 2 out of 52 out of 5     Points: 2

Populate a DataSet with multiple DataTable objects using multiple SELECT statements


Author: pappan     Member Level: Gold      Member Rank: 0     Date: 29/Mar/2008   Rating: 2 out of 52 out of 5     Points: 2

adp=new SqldataAdpter=("select * from TN1",con);
adp=new SqldataAdpter=("select * from TN2",con);
adp.Fill(DS);

DS.Tables(0).TableName = “TN1“)
Ds.Tables(1).TableName = “TN2“)



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : URGENT - how to store value of a textbox in a variable in ASP
Previous : how to rename a folder in tree view structure
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



About Us    Contact Us    Privacy Policy    Terms Of Use