C# – how to make DataSet return two related Tables

ado.netcnetsql-server-2008

I am using C# + .Net 3.5 + VSTS 2008 + ADO.Net + SQL Server 2008. I have two related tables in SQL Server (foreign key relationship). And I want to load the two tables as two datatables in a dataset. Any reference code to do this?

thanks in advance,
George

Best Answer

Try this

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“)

http://pietschsoft.com/post/2004/08/22/Fill-DataSet-with-multiple-Tables-and-update-them-with-DataAdapter.aspx