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

C#编程

开发平台:

Others

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. /// <summary>
  6. /// This class provides an example of creating and using a data grid.
  7. /// </summary>
  8. public class DataSourceDataSet : System.Windows.Forms.Form
  9. {
  10. private System.Windows.Forms.Button retrieveButton ;
  11. private System.Windows.Forms.DataGrid dataGrid ;
  12. /// <summary>
  13. /// Construct the window.
  14. /// </summary>
  15. /// <remarks>
  16. /// This method constructs the window by creating both the data grid and the button.
  17. /// </remarks>
  18. public DataSourceDataSet ( )
  19. {
  20. this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
  21. this.ClientSize = new System.Drawing.Size (464, 253);
  22. this.Text = "05_DataSourceDataSet" ;
  23. this.dataGrid = new System.Windows.Forms.DataGrid ();
  24. dataGrid.BeginInit ();
  25. dataGrid.Location = new System.Drawing.Point (8, 8);
  26. dataGrid.Size = new System.Drawing.Size (448, 208);
  27. dataGrid.TabIndex = 0;
  28. dataGrid.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right ;
  29. this.Controls.Add (this.dataGrid);
  30. dataGrid.EndInit ();
  31. this.retrieveButton = new System.Windows.Forms.Button ();
  32. retrieveButton.Location = new System.Drawing.Point (384, 224);
  33. retrieveButton.Size = new System.Drawing.Size (75, 23);
  34. retrieveButton.TabIndex = 1;
  35. retrieveButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right ;
  36. retrieveButton.Text = "Retrieve";
  37. retrieveButton.Click += new System.EventHandler (this.retrieveButton_Click);
  38. this.Controls.Add (this.retrieveButton);
  39. }
  40. /// <summary>
  41. /// Retrieve the data
  42. /// </summary>
  43. /// <param name="sender"> </param>
  44. /// <param name="e"> </param>
  45. protected void retrieveButton_Click (object sender, System.EventArgs e)
  46. {
  47. retrieveButton.Enabled = false ;
  48. string orders = "SELECT * FROM Orders";
  49. string customers = "SELECT * FROM Customers";
  50. SqlConnection con = new SqlConnection ( Login.Connection ) ;
  51. SqlDataAdapter da = new SqlDataAdapter ( orders , con ) ;
  52. DataSet ds = new DataSet ( ) ;
  53. da.Fill ( ds , "Orders" ) ;
  54. da = new SqlDataAdapter ( customers , con ) ;
  55. da.Fill ( ds , "Customers" ) ;
  56. ds.Relations.Add("CustomerOrders",
  57. ds.Tables["Customers"].Columns["CustomerID"],
  58. ds.Tables["Orders"].Columns["CustomerID"]);
  59. dataGrid.SetDataBinding ( ds , "Customers" ) ;
  60. }
  61. /// <summary>
  62. /// Display the application window
  63. /// </summary>
  64. static void Main() 
  65. {
  66. Application.Run(new DataSourceDataSet());
  67. }
  68. }