📄 datasetform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MobileDevelopersHandbook
{
public partial class DataSetForm : Form
{
public DataSetForm()
{
InitializeComponent();
}
private void menuItem1_Click(object sender, EventArgs e)
{
DoStuffWithDataSet();
}
private void DoStuffWithDataSet()
{
// Create an instance of the strongly-typed DataSet
ProductsDataSet productsDS = new ProductsDataSet();
// Create a table adapter for product categories
ProductsDataSetTableAdapters.ProductCategoryTableAdapter catTA =
new ProductsDataSetTableAdapters.ProductCategoryTableAdapter();
// ... and for products
ProductsDataSetTableAdapters.ProductTableAdapter prodTA =
new ProductsDataSetTableAdapters.ProductTableAdapter();
// FIRST: Get any existing data in these tables from the database
// Fill the ProductCategories table in the DataSet with data from the database
catTA.Fill(productsDS.ProductCategory);
// .. and the Products.
prodTA.Fill(productsDS.Product);
// Report count of records to the screen...
ReportRecords(productsDS);
// SECOND: Add some product categories
// To make sure we don't duplicate names, productCategory.Name has a unique constraint
try
{
productsDS.ProductCategory.AddProductCategoryRow("Rock Climbing Equipment");
productsDS.ProductCategory.AddProductCategoryRow("Scuba Diving Equipment");
// Update to write changes back to the database
catTA.Update(productsDS.ProductCategory);
textBox1.Text += "\r\nProduct Categories added.\r\n";
}
catch (ConstraintException)
{
// If the categories already exist, just continue
textBox1.Text += "\r\nProduct Category addition failed, items already exist.\r\n";
}
// THIRD: Add some products in the category with categoryID of 1
ProductsDataSet.ProductCategoryRow prodCatRow = productsDS.ProductCategory.FindByProductCategoryID(1);
productsDS.Product.AddProductRow("Contoso Single Rope", "Red/Blue", 155.95M, "60m", prodCatRow);
productsDS.Product.AddProductRow("Contoso Rock Shoes", "Black", 89.95M, "8", prodCatRow);
// Write to database
prodTA.Update(productsDS.Product);
textBox1.Text += "\r\nProducts added.\r\n";
// Report count of records to screen...
ReportRecords(productsDS);
}
private void ReportRecords(ProductsDataSet productsDS)
{
StringBuilder sb = new StringBuilder(textBox1.Text);
sb.Append("There are currently ");
sb.Append(productsDS.ProductCategory.Rows.Count);
sb.Append(" product categories.\r\n");
sb.Append("There are currently ");
sb.Append(productsDS.Product.Rows.Count);
sb.Append(" products.\r\n");
textBox1.Text = sb.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -