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

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.ADOSample2 
  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. this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
  91. // 
  92. // Form1
  93. // 
  94. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  95. this.ClientSize = new System.Drawing.Size(395, 488);
  96. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  97.   this.dataGrid1,
  98.   this.button1,
  99.   this.listBox1});
  100. this.Name = "Form1";
  101. this.Text = "Form1";
  102. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  103. this.ResumeLayout(false);
  104. }
  105. #endregion
  106. /// <summary>
  107. ///  The main entry point for the application.
  108. /// </summary>
  109. [STAThread]
  110. static void Main() 
  111. {
  112. try
  113. {
  114. Application.Run(new Form1());
  115. }
  116. catch(System.IO.FileNotFoundException){}
  117. }
  118. // ADOSample2/form.cs
  119. private void button1_Click(object sender, System.EventArgs e)
  120. {
  121. //create a dataset
  122. DataSet ds=new DataSet("XMLProducts");
  123. //connect to the northwind database and 
  124. //select all of the rows from products table
  125. string source = Login.Connection;
  126. SqlConnection conn=new SqlConnection(source);
  127. SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM Products",conn);
  128. //fill the dataset
  129. da.Fill(ds,"products");
  130. ds.WriteXml("sample.xml", XmlWriteMode.WriteSchema);
  131. //load data into grid
  132. dataGrid1.DataSource=ds;
  133. dataGrid1.DataMember="products";
  134. doc=new XmlDataDocument(ds);
  135. //get all of the products elements
  136. XmlNodeList nodeLst=doc.GetElementsByTagName("ProductName");
  137. //load them into the list box
  138. for(int ctr=0;ctr<nodeLst.Count;ctr++)
  139. //foreach(XmlNode node in nodeLst)
  140. // listBox1.Items.Add(node.InnerText);
  141. listBox1.Items.Add(nodeLst[ctr].InnerText);
  142. }
  143.     
  144. private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  145. {
  146. //when you click on the listbox.
  147. //a message box appears with unit price
  148. string srch="XMLProducts/products[ProductName=" + '"' + listBox1.SelectedItem.ToString() + '"' + "]";
  149. XmlNode foundNode=doc.SelectSingleNode(srch);
  150. if(foundNode!=null)
  151. MessageBox.Show(foundNode.SelectSingleNode("UnitPrice").InnerText);
  152. else
  153. MessageBox.Show("Not found");
  154.       
  155. }
  156. }
  157. }