Author: veeresh Member Level: Diamond Member Rank: 0 Date: 23/Jan/2007 Rating:  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:  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:  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:  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:  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:  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“)
|