Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using Microsoft.VisualBasic;
- //
- //加入參考 Mircosoft.VisualBasic,
- //就可以使用Microsoft.VisualBasic.Interaction.InputBox("測試", "標頭", "預設值", 400, 300);
- namespace WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- button1.Text = "建立DataSet";
- }
- private void button1_Click(object sender, EventArgs e)
- {
- DataSet dsKings = new DataSet("KINGS");
- //new 一個 DataSet
- DataTable dtProduct = new DataTable("Product");
- // new 一個 Table
- dsKings.Tables.Add(dtProduct);
- //將 Table 加入 DataSet
- int i;
- for (i = 0; i <= dsKings.Tables.Count - 1; i++)
- {
- listBox1.Items.Add(dsKings.Tables[i].TableName);
- }
- //new Column
- DataColumn colProductID = new DataColumn("ProductID");
- colProductID.DataType = System.Type.GetType("System.String");
- colProductID.MaxLength = 10;
- colProductID.AllowDBNull = false;
- //商品編號
- DataColumn colProductName = new DataColumn("ProductName");
- colProductName.DataType = System.Type.GetType("System.String");
- colProductName.MaxLength = 40;
- colProductName.AllowDBNull = false;
- colProductName.Unique = true;
- //商品編號
- DataColumn colAuthor = new DataColumn("Author");
- colAuthor.DataType = System.Type.GetType("System.String");
- colAuthor.MaxLength = 12;
- colAuthor.AllowDBNull = false;
- //作者
- DataColumn colStore = new DataColumn("Store");
- colStore.DataType = System.Type.GetType("System.String");
- colStore.MaxLength = 20;
- colStore.AllowDBNull = false;
- //出版社
- DataColumn colTotalOrderQuantity = new DataColumn("TotalOrderQuantity");
- colTotalOrderQuantity.DataType = System.Type.GetType("System.String");
- colTotalOrderQuantity.DefaultValue = 0;
- colTotalOrderQuantity.AllowDBNull = false;
- //累積訂購量
- dtProduct.Columns.Add(colProductID);
- dtProduct.Columns.Add(colProductName);
- dtProduct.Columns.Add(colAuthor);
- dtProduct.Columns.Add(colStore);
- dtProduct.Columns.Add(colTotalOrderQuantity);
- //將 Column 加入 Table
- dtProduct.PrimaryKey = new DataColumn[] { dtProduct.Columns["colProductID"] };
- // set PrimaryKey
- foreach (Constraint ProductConstraint in dtProduct.Constraints )
- {
- listBox1.Items.Add("條件約束:" + ProductConstraint.ConstraintName.ToString());
- }
- DataRow drNewProduct;
- //建立DataRow物件
- //新增方式 A
- drNewProduct = dtProduct.NewRow();
- //再新的DataRow給予值
- drNewProduct["ProductID"] = "P2297";
- drNewProduct["ProductName"] = "Visual Basic .NET 近銷存程式設計01-A";
- drNewProduct["Author"] = "阿惟01-A";
- drNewProduct["Store"] = "文魁資訊01-A";
- dtProduct.Rows.Add(drNewProduct);
- //將 DataRow 加入資料表
- //新增方式 B
- drNewProduct = dtProduct.NewRow();
- //再新的DataRow給予值
- drNewProduct[0] = "P7117";
- drNewProduct[1] = "Visual Basic 2005 資料庫程式設計02-A";
- drNewProduct[2] = "阿惟02-A";
- drNewProduct[3] = "文魁資訊02-A";
- dtProduct.Rows.Add(drNewProduct);
- //將 DataRow 加入資料表
- //新增方式 C
- dtProduct.Rows.Add(new object [] { "P999", "Access 2007近銷存管理系統實作03-A", "阿惟03-A", "文魁資訊03-A" });
- dataGridView1.DataSource = dsKings;
- //用 datagridview1 接 DataSet
- dataGridView1.DataMember = "Product";
- //選定 Table
- }
- }
- }
Add Comment
Please, Sign In to add comment