创建DataTable

微软官方文档上有很多示例,现截取一个:

// Create a DataSet with two tables and populate it.
        private void MakeDataSet()
        {
            // Create a DataSet.
            ds = new DataSet("myDataSet");

            // Create two DataTables.
            DataTable tCust = new DataTable("Customers");
            DataTable tOrders = new DataTable("Orders");

            // Create two columns, and add them to the first table.
            DataColumn cCustID = new DataColumn("CustID", typeof(int));
            DataColumn cCustName = new DataColumn("CustName");
            tCust.Columns.Add(cCustID);
            tCust.Columns.Add(cCustName);

            // Create three columns, and add them to the second table.
            DataColumn cID =
               new DataColumn("CustID", typeof(int));
            DataColumn cOrderDate =
               new DataColumn("orderDate", typeof(DateTime));
            DataColumn cOrderAmount =
               new DataColumn("OrderAmount", typeof(decimal));
            tOrders.Columns.Add(cOrderAmount);
            tOrders.Columns.Add(cID);
            tOrders.Columns.Add(cOrderDate);

            // Add the tables to the DataSet.
            ds.Tables.Add(tCust);
            ds.Tables.Add(tOrders);

            // Create a DataRelation, and add it to the DataSet.
            DataRelation dr = new DataRelation
            ("custToOrders", cCustID, cID);
            ds.Relations.Add(dr);

            /* Populate the tables. For each customer and order, 
               create two DataRow variables. */
            DataRow newRow1;
            DataRow newRow2;

            // Create three customers in the Customers Table.
            for (int i = 1; i < 4; i++)
            {
                newRow1 = tCust.NewRow();
                newRow1["custID"] = i;
                // Add the row to the Customers table.
                tCust.Rows.Add(newRow1);
            }
            // Give each customer a distinct name.
            tCust.Rows[0]["custName"] = "Alpha";
            tCust.Rows[1]["custName"] = "Beta";
            tCust.Rows[2]["custName"] = "Omega";

            // For each customer, create five rows in the Orders table.
            for (int i = 1; i < 4; i++)
            {
                for (int j = 1; j < 6; j++)
                {
                    newRow2 = tOrders.NewRow();
                    newRow2["CustID"] = i;
                    newRow2["orderDate"] = new DateTime(2001, i, j * 2);
                    newRow2["OrderAmount"] = i * 10 + j * .1;
                    // Add the row to the Orders table.
                    tOrders.Rows.Add(newRow2);
                }
            }
        }

注:有不懂,找官方文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值