ADOSample3.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.ADOSample3 
  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. //ADOSample3form.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 and from suppliers table
  125. //make sure you connect string matches you server configuration
  126. string source = Login.Connection;
  127. SqlConnection conn=new SqlConnection(source);
  128. SqlDataAdapter daProd=new SqlDataAdapter("SELECT * FROM Products",conn);
  129. SqlDataAdapter daSup=new SqlDataAdapter("SELECT * FROM Suppliers",conn);
  130. //Fill DataSet from both SqlAdapters
  131. daProd.Fill(ds,"products");
  132. daSup.Fill(ds,"suppliers");
  133. //Add the relation
  134. ds.Relations.Add(ds.Tables["suppliers"].Columns["SupplierId"],
  135. ds.Tables["products"].Columns["SupplierId"]);
  136. //Write the Xml to a file so we can look at it later
  137. ds.WriteXml("..\SuppProd.xml",XmlWriteMode.WriteSchema);
  138. //load data into grid
  139. dataGrid1.DataSource=ds;
  140. dataGrid1.DataMember="suppliers";
  141. //create the XmlDataDocument
  142. doc=new XmlDataDocument(ds);
  143. //Select the productname elements and load them in the grid
  144. XmlNodeList nodeLst=doc.SelectNodes("//ProductName");
  145.      
  146. foreach(XmlNode nd in nodeLst)
  147. listBox1.Items.Add(nd.InnerXml);
  148.     
  149. }
  150.     
  151. private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  152. {
  153. string srch="XMLProducts/products[ProductName=" + '"' + listBox1.SelectedItem.ToString() + '"' + "]";
  154. XmlNode foundNode=doc.SelectSingleNode(srch);
  155. if(foundNode!=null)
  156. MessageBox.Show(foundNode.SelectSingleNode("UnitPrice").InnerText);
  157. else
  158. MessageBox.Show("Not found");
  159.       
  160. }
  161. }
  162. }