I made an example where I use two Tableadapters in 1 Dataset
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
string customerCommandText = "select * from customers";
SqlDataAdapter customerAdapter = new SqlDataAdapter(customerCommandText, connection);
string ordersCommandText = "select * from Orders";
SqlDataAdapter ordersAdapter = new SqlDataAdapter(ordersCommandText, connection);
DataSet customerOrders = new DataSet();
customerAdapter.Fill(customerOrders, "Customers");
ordersAdapter.Fill(customerOrders, "Orders");
DataRelation relation = customerOrders.Relations.Add("CustomerOrders",
customerOrders.Tables["Customers"].Columns["CustomerId"],
customerOrders.Tables["Orders"].Columns["CustomerId"]);
string json = JsonConvert.SerializeObject(customerOrders, Formatting.Indented);
}
DDL of the tables
CREATE TABLE [dbo].[Customers] (
[CustomerId] [int] NOT NULL IDENTITY,
[Valor] [decimal](18, 2) NOT NULL,
CONSTRAINT [PK_dbo.Customers] PRIMARY KEY ([CustomerId])
)
CREATE TABLE [dbo].[Orders] (
[OrderId] [int] NOT NULL IDENTITY,
[CustomerId] [int] NOT NULL,
[Teste] [nvarchar](max),
CONSTRAINT [PK_dbo.Orders] PRIMARY KEY ([OrderId])
)
Serialized dataset
{
"Customers": [
{
"CustomerId": 1,
"Valor": 920.00
},
{
"CustomerId": 2,
"Valor": 930.00
},
{
"CustomerId": 3,
"Valor": 932.00
}
],
"Orders": [
{
"OrderId": 1,
"CustomerId": 1,
"Teste": "636510290829216964"
},
{
"OrderId": 2,
"CustomerId": 1,
"Teste": "636510290829216964"
},
{
"OrderId": 3,
"CustomerId": 1,
"Teste": "636510290829216964"
},
{
"OrderId": 4,
"CustomerId": 2,
"Teste": "636510290839303071"
},
{
"OrderId": 5,
"CustomerId": 2,
"Teste": "636510290839303071"
},
{
"OrderId": 6,
"CustomerId": 2,
"Teste": "636510290839303071"
},
{
"OrderId": 7,
"CustomerId": 3,
"Teste": "636510290849322161"
},
{
"OrderId": 8,
"CustomerId": 3,
"Teste": "636510290849322161"
},
{
"OrderId": 9,
"CustomerId": 3,
"Teste": "636510290849322161"
}
]
}
What didn’t work out?
– novic