.Net C # DataTables and DataSets, How to Link Tables

How do you take a couple of data tables and put them in a data set and link (which doesn't even look like the right English)?

I know how to create datatables.

+3
source share
7 answers

Here is an example from one of my classes

// create the relationship between Booking and Booking_MNI
DataRelation relBookingMNI;                         
relBookingMNI = new DataRelation("BookingToBookingMNI",dsBooking.Tables["Booking"].Columns["Record_Id"],dsBooking.Tables["Booking_MNI"].Columns["booking_record_id"]);
dsBooking.Relations.Add(relBookingMNI);

dsBooking is my main dataset that contains 2 tables. Reservation and reservation_MNI If Record_Id is the primary key and reserve_record_id is the foreign key

, . , , . "+" , . , , , .

DataTable dtBooking = ds.Tables[0];
DataTable dtBooking_MNI = ds.Tables[1];

dtBooking.PrimaryKey = new DataColumn[] {dtBooking.Columns["Record_Id"]};
dtBooking_MNI.PrimaryKey = new DataColumn[] {dtBooking_MNI.Columns["booking_Record_Id"]};

/* Setup DataRelation between the DataTables */
DataColumn[] dcBookingColsArray = new DataColumn[1] {dtBooking.Columns["Record_Id"]};
DataColumn[] dcBookingMNIColsArray = new DataColumn[1] {dtBooking_MNI.Columns["booking_record_Id"]};

DataRelation relBooking_To_MNI = new DataRelation("Booking_To_MNI",dcBookingColsArray,dcBookingMNIColsArray);
ds.Relations.Add(relBooking_To_MNI_Units);

// grid where you want to display the relationship
grdBooking.DataSource = ds;
+9

DataRelation. , DataSet .

+3

, DataTables "" "orderDetails". OrderNumber. , , orderDetails - . .

DataSet orderData = new DataSet("OrderData");

orderData.Tables.Add(orders);
orderData.Tables.Add(orderDetails);

orderData.Relations.Add("Order_OrderDetails", orders.Columns["OrderNumber"], orderDetails.Columns["OrderNumber"]);

, - :

DataRelation orderRelation = orderData.Relations["Order_OrderDetails"];

foreach (DataRow order in orders.Rows)
{
   Console.WriteLine("Subtotals for Order {0}:", order["OrderNumber"]);

   foreach (DataRow orderDetail in order.GetChildRows(orderRelation))
   {
      Console.WriteLine("Order Line {0}: {1}", orderDetail["OrderLineNumber"], string.Format("{0:C}", orderDetail["Price"]));
   }
}
+2

1. 2.

        string query = "SELECT * FROM Categories; SELECT * FROM Products";

        SqlConnection con = new SqlConnection();
        SqlDataAdapter da = new SqlDataAdapter(query,con);
        DataSet ds = new DataSet();
        da.Fill(ds, "CategoriesAndProducts");     //CategoriesAndProducts dataset

        string s = ds.Tables[0].Rows[0]["Name"].ToString();  
        string s1 = ds.Tables[1].Rows[0]["Name"].ToString(); 

        Console.WriteLine(s);  //from categories [0][0] like Electronic
        Console.WriteLine(s1); //from Products  [0][0]  like LG
+1

Visual Studio 2005 , : "/Item...", "DataSet" , xsd . , ( , / ...) . [YourNewDataSet}.Designer.cs . - :

this.relationFK_DataTable2_DataTable1 = new global::System.Data.DataRelation("FK_DataTable2_DataTable1", new global::System.Data.DataColumn[] {
                    this.tableDataTable2.asdfasColumn}, new global::System.Data.DataColumn[] {
                    this.tableDataTable1.asdfaColumn}, false);

, , .

0

Source: https://habr.com/ru/post/1712506/


All Articles