Guest User

Untitled

a guest
Oct 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using Microsoft.VisualBasic;
  9. //
  10. //加入參考 Mircosoft.VisualBasic,
  11. //就可以使用Microsoft.VisualBasic.Interaction.InputBox("測試", "標頭", "預設值", 400, 300);
  12.  
  13. namespace WindowsFormsApplication2
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. button1.Text = "建立DataSet";
  25. }
  26.  
  27. private void button1_Click(object sender, EventArgs e)
  28. {
  29. DataSet dsKings = new DataSet("KINGS");
  30. //new 一個 DataSet
  31. DataTable dtProduct = new DataTable("Product");
  32. // new 一個 Table
  33.  
  34. dsKings.Tables.Add(dtProduct);
  35. //將 Table 加入 DataSet
  36.  
  37. int i;
  38. for (i = 0; i <= dsKings.Tables.Count - 1; i++)
  39. {
  40. listBox1.Items.Add(dsKings.Tables[i].TableName);
  41. }
  42.  
  43. //new Column
  44. DataColumn colProductID = new DataColumn("ProductID");
  45. colProductID.DataType = System.Type.GetType("System.String");
  46. colProductID.MaxLength = 10;
  47. colProductID.AllowDBNull = false;
  48. //商品編號
  49.  
  50. DataColumn colProductName = new DataColumn("ProductName");
  51. colProductName.DataType = System.Type.GetType("System.String");
  52. colProductName.MaxLength = 40;
  53. colProductName.AllowDBNull = false;
  54. colProductName.Unique = true;
  55. //商品編號
  56.  
  57. DataColumn colAuthor = new DataColumn("Author");
  58. colAuthor.DataType = System.Type.GetType("System.String");
  59. colAuthor.MaxLength = 12;
  60. colAuthor.AllowDBNull = false;
  61. //作者
  62.  
  63. DataColumn colStore = new DataColumn("Store");
  64. colStore.DataType = System.Type.GetType("System.String");
  65. colStore.MaxLength = 20;
  66. colStore.AllowDBNull = false;
  67. //出版社
  68.  
  69. DataColumn colTotalOrderQuantity = new DataColumn("TotalOrderQuantity");
  70. colTotalOrderQuantity.DataType = System.Type.GetType("System.String");
  71. colTotalOrderQuantity.DefaultValue = 0;
  72. colTotalOrderQuantity.AllowDBNull = false;
  73. //累積訂購量
  74.  
  75.  
  76.  
  77. dtProduct.Columns.Add(colProductID);
  78. dtProduct.Columns.Add(colProductName);
  79. dtProduct.Columns.Add(colAuthor);
  80. dtProduct.Columns.Add(colStore);
  81. dtProduct.Columns.Add(colTotalOrderQuantity);
  82. //將 Column 加入 Table
  83.  
  84. dtProduct.PrimaryKey = new DataColumn[] { dtProduct.Columns["colProductID"] };
  85. // set PrimaryKey
  86.  
  87. foreach (Constraint ProductConstraint in dtProduct.Constraints )
  88. {
  89. listBox1.Items.Add("條件約束:" + ProductConstraint.ConstraintName.ToString());
  90.  
  91. }
  92.  
  93. DataRow drNewProduct;
  94. //建立DataRow物件
  95.  
  96. //新增方式 A
  97. drNewProduct = dtProduct.NewRow();
  98. //再新的DataRow給予值
  99. drNewProduct["ProductID"] = "P2297";
  100. drNewProduct["ProductName"] = "Visual Basic .NET 近銷存程式設計01-A";
  101. drNewProduct["Author"] = "阿惟01-A";
  102. drNewProduct["Store"] = "文魁資訊01-A";
  103. dtProduct.Rows.Add(drNewProduct);
  104. //將 DataRow 加入資料表
  105.  
  106. //新增方式 B
  107. drNewProduct = dtProduct.NewRow();
  108. //再新的DataRow給予值
  109. drNewProduct[0] = "P7117";
  110. drNewProduct[1] = "Visual Basic 2005 資料庫程式設計02-A";
  111. drNewProduct[2] = "阿惟02-A";
  112. drNewProduct[3] = "文魁資訊02-A";
  113. dtProduct.Rows.Add(drNewProduct);
  114. //將 DataRow 加入資料表
  115.  
  116. //新增方式 C
  117. dtProduct.Rows.Add(new object [] { "P999", "Access 2007近銷存管理系統實作03-A", "阿惟03-A", "文魁資訊03-A" });
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. dataGridView1.DataSource = dsKings;
  125. //用 datagridview1 接 DataSet
  126. dataGridView1.DataMember = "Product";
  127. //選定 Table
  128.  
  129. }
  130. }
  131. }
Add Comment
Please, Sign In to add comment