示例#1
0
        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());
        }
示例#2
0
        private static void Demo1()
        {
            ExampleDb.CustomersDataTable sss = new ExampleDb.CustomersDataTable();
            ExampleDbTableAdapters.CustomersTableAdapter ss = new ExampleDbTableAdapters.CustomersTableAdapter();
            ss.Fill(sss);
            StringBuilder sbBuilder = new StringBuilder();

            foreach (ExampleDb.CustomersRow ct in sss.Rows)
            {
                sbBuilder.Append(ct.CustomerID + "  " + ct.CompanyName + Environment.NewLine);
            }
            MessageBox.Show(sbBuilder.ToString());
        }
示例#3
0
        private static void Demo2()
        {
            ExampleDb nds = new ExampleDb();

            ExampleDbTableAdapters.CustomersTableAdapter ndsAdapter = new ExampleDbTableAdapters.CustomersTableAdapter();
            ndsAdapter.Fill(nds.Customers);

            StringBuilder sbBuilder = new StringBuilder();

            foreach (ExampleDb.CustomersRow ct in nds.Customers.Rows)
            {
                sbBuilder.Append(ct.CustomerID + "  " + ct.CompanyName + Environment.NewLine);
            }

            DataRow row = nds.Customers.Rows.Find(1);

            sbBuilder.Append(row["CompanyName"]);
            MessageBox.Show(sbBuilder.ToString());
        }