ADOSample6.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:6k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Xml;
  8. using System.Data.SqlClient;
  9. using System.IO;
  10. namespace Wrox.ProCSharp.Xml.ADOSample6
  11. {
  12. /// <summary>
  13. /// Summary description for Form1.
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.ListBox listBox1;
  18. private System.Windows.Forms.Button button1;
  19. private System.Windows.Forms.DataGrid dataGrid1;
  20. /// <summary>
  21. /// Required designer variable.
  22. /// </summary>
  23. private System.ComponentModel.Container components = null;
  24. private XmlDocument doc=new XmlDocument();
  25. public Form1()
  26. {
  27. //
  28. // Required for Windows Form Designer support
  29. //
  30. InitializeComponent();
  31. //
  32. // TODO: Add any constructor code after InitializeComponent call
  33. //
  34. }
  35. /// <summary>
  36. /// Clean up any resources being used.
  37. /// </summary>
  38. protected override void Dispose( bool disposing )
  39. {
  40. if( disposing )
  41. {
  42. if (components != null) 
  43. {
  44. components.Dispose();
  45. }
  46. }
  47. base.Dispose( disposing );
  48. }
  49. #region Windows Form Designer generated code
  50. /// <summary>
  51. /// Required method for Designer support - do not modify
  52. /// the contents of this method with the code editor.
  53. /// </summary>
  54. private void InitializeComponent()
  55. {
  56. this.dataGrid1 = new System.Windows.Forms.DataGrid();
  57. this.button1 = new System.Windows.Forms.Button();
  58. this.listBox1 = new System.Windows.Forms.ListBox();
  59. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  60. this.SuspendLayout();
  61. // 
  62. // dataGrid1
  63. // 
  64. this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  65. | System.Windows.Forms.AnchorStyles.Left) 
  66. | System.Windows.Forms.AnchorStyles.Right);
  67. this.dataGrid1.DataMember = "";
  68. this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  69. this.dataGrid1.Location = new System.Drawing.Point(0, 248);
  70. this.dataGrid1.Name = "dataGrid1";
  71. this.dataGrid1.Size = new System.Drawing.Size(392, 176);
  72. this.dataGrid1.TabIndex = 2;
  73. // 
  74. // button1
  75. // 
  76. this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  77. this.button1.Location = new System.Drawing.Point(164, 432);
  78. this.button1.Name = "button1";
  79. this.button1.TabIndex = 1;
  80. this.button1.Text = "Load Data";
  81. this.button1.Click += new System.EventHandler(this.button1_Click);
  82. // 
  83. // listBox1
  84. // 
  85. this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  86. | System.Windows.Forms.AnchorStyles.Right);
  87. this.listBox1.Name = "listBox1";
  88. this.listBox1.Size = new System.Drawing.Size(392, 238);
  89. this.listBox1.TabIndex = 0;
  90. // 
  91. // Form1
  92. // 
  93. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  94. this.ClientSize = new System.Drawing.Size(395, 488);
  95. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  96.   this.dataGrid1,
  97.   this.button1,
  98.   this.listBox1});
  99. this.Name = "Form1";
  100. this.Text = "Form1";
  101. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  102. this.ResumeLayout(false);
  103. }
  104. #endregion
  105. /// <summary>
  106. ///  The main entry point for the application.
  107. /// </summary>
  108. [STAThread]
  109. static void Main() 
  110. {
  111. try
  112. {
  113. Application.Run(new Form1());
  114. }
  115. catch(System.IO.FileNotFoundException){}
  116. }
  117.   //ADOSample6form.cs
  118.     private void button1_Click(object sender, System.EventArgs e)
  119.     {
  120.       //new DataSet
  121.       DataSet ds=new DataSet("XMLProducts");
  122.       //Make connection and load products rows
  123.       string source=Login.Connection;
  124.       
  125.       SqlConnection conn=new SqlConnection(source);
  126.       
  127.       SqlDataAdapter da=new SqlDataAdapter("select * from products",conn);
  128.       //fill the DataSet
  129.       da.Fill(ds,"products");
  130.       //edit first row
  131.       ds.Tables["products"].Rows[0]["ProductName"]="NewProdName";
  132.       //add new row
  133.       DataRow dr=ds.Tables["products"].NewRow();;
  134.       dr["ProductId"]=100;
  135.       dr["CategoryId"]=2;
  136.       dr["Discontinued"]=false;
  137.       dr["ProductName"]="This is the new product";
  138.       dr["QuantityPerUnit"]=12;
  139.       dr["ReorderLevel"]=1;
  140.       dr["SupplierId"]=12;
  141.       dr["UnitPrice"]=23;
  142.       dr["UnitsInStock"]=5;
  143.       dr["UnitsOnOrder"]=0;
  144.       
  145.       ds.Tables["products"].Rows.Add(dr);
  146.       //Write the Schema 
  147.       ds.WriteXmlSchema("..\diffgram.xsd");
  148.       //generate the DiffGram
  149.       ds.WriteXml("..\diffgram.xml",XmlWriteMode.DiffGram);
  150.       ds.AcceptChanges();          
  151.       
  152.       //load data into grid
  153.       dataGrid1.DataSource=ds;
  154.       dataGrid1.DataMember="products";
  155.       //new XmlDataDocument
  156.       doc=new XmlDataDocument(ds);
  157.       //load the productnames in the list
  158.       XmlNodeList nodeLst=doc.SelectNodes("//ProductName");
  159.          
  160.       foreach(XmlNode nd in nodeLst)
  161.         listBox1.Items.Add(nd.InnerXml);
  162.     
  163.     }
  164.     
  165.     private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  166.     {
  167.       string srch="XMLProducts/products[ProductName=" + '"' + listBox1.SelectedItem.ToString() + '"' + "]";
  168.       XmlNode foundNode=doc.SelectSingleNode(srch);
  169.       if(foundNode!=null)
  170.         MessageBox.Show(foundNode.SelectSingleNode("UnitPrice").InnerText);
  171.       else
  172.         MessageBox.Show("Not found");
  173.       
  174.     }
  175. }
  176. }