public static void Demo4() { ExampleDb nds = new ExampleDb(); ExampleDbTableAdapters.CustomersTableAdapter ndsAdapter = new ExampleDbTableAdapters.CustomersTableAdapter(); ndsAdapter.Fill(nds.Customers); ExampleDbTableAdapters.OrdersTableAdapter ordTblApt = new ExampleDbTableAdapters.OrdersTableAdapter(); ordTblApt.Fill(nds.Orders); //通过Customers表中的CustomerID(主键)查找Orders表中对应的数据 ExampleDb.CustomersRow cusRow1 = (ExampleDb.CustomersRow)nds.Customers.Rows.Find(1); ExampleDb.CustomersRow cusRow2 = nds.Customers.FindByCustomerID(1); StringBuilder sbBuilder = new StringBuilder(); foreach (var rowResult in cusRow1.GetOrdersRows()) { sbBuilder.Append(rowResult.OrderID + " " + rowResult.OrderDate + " " + rowResult.CustomerID + Environment.NewLine); } foreach (var rowResult in cusRow2.GetOrdersRows()) { sbBuilder.Append(rowResult.OrderID + " " + rowResult.OrderDate + " " + rowResult.CustomerID + Environment.NewLine); } //通过Orders表中的CustomerID(外键)查找Customers表中的数据 ExampleDb.OrdersRow ordRowResult = (ExampleDb.OrdersRow)nds.Orders.Rows.Find(1); sbBuilder.Append(ordRowResult.CustomerID + Environment.NewLine); sbBuilder.Append(ordRowResult.OrderDate + Environment.NewLine); //修改数据 ExampleDb.CustomersRow cr = ordRowResult.CustomersRow; sbBuilder.Append(cr.CustomerID); sbBuilder.Append(cr.CompanyName); cr.CompanyName = "name19:44"; ndsAdapter.Update(cr); Console.WriteLine(cr.CompanyName); //修改数据 ExampleDb.CustomersRow cusRow = nds.Customers.FindByCustomerID(1); cusRow.CompanyName = "MyCompanyName"; ndsAdapter.Update(cusRow); Console.WriteLine(cusRow.CompanyName); //添加一行数据 ExampleDb.CustomersRow newRow = nds.Customers.AddCustomersRow("aMyName"); ndsAdapter.Update(newRow); //删除一行数据 ndsAdapter.Delete(4, "aMyName"); MessageBox.Show(sbBuilder.ToString()); }
private static void Demo3() { ExampleDb nds = new ExampleDb(); ExampleDbTableAdapters.OrdersTableAdapter ordTblApt = new ExampleDbTableAdapters.OrdersTableAdapter(); ordTblApt.Fill(nds.Orders); StringBuilder sbBuilder = new StringBuilder(); foreach (ExampleDb.OrdersRow ordRow in nds.Orders.Rows) { sbBuilder.Append(ordRow.OrderID + " " + ordRow.CustomerID + " " + ordRow.OrderDate + Environment.NewLine); } MessageBox.Show(sbBuilder.ToString()); }