ADOSample1.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.ADOSample1 
  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. // ADOSample1/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.             
  126. // We use the connection string from the file Login.cs
  127.             string source = Login.Connection;
  128. SqlConnection conn=new SqlConnection(source);
  129. SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM Products" , conn);
  130.       
  131. //use memory stream so we don't have to create temp files
  132.       
  133. MemoryStream memStrm=new MemoryStream();
  134. StreamReader strmRead=new StreamReader(memStrm);
  135. StreamWriter strmWrite=new StreamWriter(memStrm);
  136. //fill the dataset
  137. da.Fill(ds,"Products");
  138.   //load data into grid
  139. dataGrid1.DataSource=ds;
  140. dataGrid1.DataMember="Products";
  141. //write the xml from the dataset to the memory stream
  142. ds.WriteXml(strmWrite,XmlWriteMode.IgnoreSchema);
  143. memStrm.Seek(0,SeekOrigin.Begin);
  144. //read from the memory stream to a XmlDocument object
  145. doc.Load(strmRead);
  146. //get all of the products elements
  147. XmlNodeList nodeLst=doc.SelectNodes("//ProductName");
  148. //load them into the list box
  149. foreach(XmlNode nd in nodeLst)
  150. listBox1.Items.Add(nd.InnerText);
  151. }
  152. private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  153. {
  154. //when you click on the listbox.
  155. //a message box appears with unit price
  156. string srch="XMLProducts/Products[ProductName=" + '"' + listBox1.SelectedItem.ToString() + '"' + "]";
  157. XmlNode foundNode=doc.SelectSingleNode(srch);
  158. if(foundNode!=null)
  159. MessageBox.Show(foundNode.SelectSingleNode("UnitPrice").InnerText);
  160. else
  161. MessageBox.Show("Not found");
  162. }
  163. }
  164. }