⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-14-05

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 14-05: Using a DataGrid with two tables

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ProgrammingCSharpWindows.Form
{
   using System.Data.SqlClient;

   public class ADOForm1 : System.Windows.Forms.Form
   {
      private System.ComponentModel.Container components;
      private System.Windows.Forms.DataGrid dataGrid1;

      // private System.Data.ADO.ADOConnection myConnection;
      private System.Data.SqlClient.SqlConnection myConnection;
      private System.Data.DataSet myDataSet;
      private System.Data.SqlClient.SqlCommand myCommand;
      private System.Data.SqlClient.SqlCommand myCommand2;
      private System.Data.SqlClient.SqlDataAdapter DataAdapter;
      private System.Data.SqlClient.SqlDataAdapter DataAdapter2;
     
    
      public ADOForm1()
      {
         InitializeComponent();


         // create the connection
         string connectionString = 
            "server=Neptune; uid=sa; pwd=oWenmEany; database=northwind";         
         myConnection = new 
            System.Data.SqlClient.SqlConnection(connectionString);
         myConnection.Open();

         // create the data set
         myDataSet = new System.Data.DataSet();
         myDataSet.CaseSensitive=true;

         // set up the command and DataSet command for the first table
         myCommand = new System.Data.SqlClient.SqlCommand();
         myCommand.Connection=myConnection;
         myCommand.CommandText = "Select * from Customers";
         DataAdapter = new System.Data.SqlClient.SqlDataAdapter();
         DataAdapter.SelectCommand= myCommand;
         DataAdapter.TableMappings.Add("Table","Customers");
         DataAdapter.Fill(myDataSet);

         // set up the command and DataSet command for the second table
         myCommand2 = new System.Data.SqlClient.SqlCommand();
         DataAdapter2 = new System.Data.SqlClient.SqlDataAdapter();
         myCommand2.Connection = myConnection;
         myCommand2.CommandText = "SELECT * FROM Orders";
         DataAdapter2.SelectCommand = myCommand2;
         DataAdapter2.TableMappings.Add ("Table", "Orders");
         DataAdapter2.Fill(myDataSet);


         // establish the relationship between the tables
         System.Data.DataRelation dataRelation;
         System.Data.DataColumn dataColumn1;
         System.Data.DataColumn dataColumn2;
         dataColumn1 = 
            myDataSet.Tables["Customers"].Columns["CustomerID"];
         dataColumn2 = 
            myDataSet.Tables["Orders"].Columns["CustomerID"];
            
         dataRelation = 
            new System.Data.DataRelation(
            "CustomersToOrders", 
            dataColumn1, 
            dataColumn2);

         // add the relation object to the data set
         myDataSet.Relations.Add(dataRelation);

         // set up the grid's view and member data and display it
         DataViewManager DataSetView = 
            myDataSet.DefaultViewManager; 
         dataGrid1.DataSource = DataSetView;
         dataGrid1.DataMember= "Customers";
      }

      public override void Dispose()
      {
         base.Dispose();
         components.Dispose();
      }

      private void InitializeComponent()
      {
         this.components = new System.ComponentModel.Container ();
         this.dataGrid1 = new System.Windows.Forms.DataGrid ();
         dataGrid1.BeginInit ();
         //@this.TrayHeight = 0;
         //@this.TrayLargeIcon = false;
         //@this.TrayAutoArrange = true;
         dataGrid1.Location = new System.Drawing.Point (24, 32);
         dataGrid1.Size = new System.Drawing.Size (480, 408);
         dataGrid1.DataMember = "";
         dataGrid1.TabIndex = 0;
         this.Text = "ADOFrm1";
         this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
         this.ClientSize = new System.Drawing.Size (536, 501);
         this.Controls.Add (this.dataGrid1);
         dataGrid1.EndInit ();
      }

 
      public static void Main(string[] args) 
      {
         Application.Run(new ADOForm1());
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -