datasets.cs

来自「东软内部材料(六)ado .net相关」· CS 代码 · 共 420 行 · 第 1/2 页

CS
420
字号
			this.cmdSelectCustomers.CommandText = "SELECT CustomerID, CompanyName, City, Country, PostalCode, Region FROM CustomerLi" +
				"st";
			this.cmdSelectCustomers.Connection = this.cnNorthwind;
			// 
			// daCustomers
			// 
			this.daCustomers.SelectCommand = this.cmdSelectCustomers;
			this.daCustomers.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																								  new System.Data.Common.DataTableMapping("Table", "CustomerList", new System.Data.Common.DataColumnMapping[0])});
			// 
			// cmdSelectOrders
			// 
			this.cmdSelectOrders.CommandText = "SELECT EmployeeID, CustomerID, OrderID, OrderDate, Subtotal FROM OrderTotals";
			this.cmdSelectOrders.Connection = this.cnNorthwind;
			// 
			// daOrders
			// 
			this.daOrders.SelectCommand = this.cmdSelectOrders;
			this.daOrders.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																							   new System.Data.Common.DataTableMapping("Table", "OrderTotals", new System.Data.Common.DataColumnMapping[0])});
			// 
			// cmdSelectEmployees
			// 
			this.cmdSelectEmployees.CommandText = "SELECT EmployeeID, FirstName, LastName FROM EmployeeList";
			this.cmdSelectEmployees.Connection = this.cnNorthwind;
			// 
			// daEmployees
			// 
			this.daEmployees.SelectCommand = this.cmdSelectEmployees;
			this.daEmployees.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																								  new System.Data.Common.DataTableMapping("Table", "Employees", new System.Data.Common.DataColumnMapping[0])});
			// 
			// dsMaster1
			// 
			this.dsMaster1.DataSetName = "dsMaster";
			this.dsMaster1.Locale = new System.Globalization.CultureInfo("en-US");
			this.dsMaster1.Namespace = "http://www.tempuri.org/dsMaster.xsd";
			// 
			// dsUntyped
			// 
			this.dsUntyped.DataSetName = "dsUntyped";
			this.dsUntyped.Locale = new System.Globalization.CultureInfo("en-US");
			this.dsUntyped.Relations.AddRange(new System.Data.DataRelation[] {
																				 new System.Data.DataRelation("MasterChild", "dtMaster", "dtChild", new string[] {
																																									 "MasterID"}, new string[] {
																																																   "MasterLink"}, false)});
			this.dsUntyped.Tables.AddRange(new System.Data.DataTable[] {
																		   this.dtMaster,
																		   this.dtChild});
			// 
			// dtMaster
			// 
			this.dtMaster.Columns.AddRange(new System.Data.DataColumn[] {
																			this.MasterID,
																			this.MasterValue});
			this.dtMaster.Constraints.AddRange(new System.Data.Constraint[] {
																				new System.Data.UniqueConstraint("Constraint1", new string[] {
																																				 "MasterID"}, false)});
			this.dtMaster.TableName = "dtMaster";
			// 
			// MasterID
			// 
			this.MasterID.AllowDBNull = false;
			this.MasterID.AutoIncrement = true;
			this.MasterID.Caption = "MasterID";
			this.MasterID.ColumnName = "MasterID";
			this.MasterID.DataType = typeof(int);
			// 
			// MasterValue
			// 
			this.MasterValue.Caption = "MasterValue";
			this.MasterValue.ColumnName = "MasterValue";
			// 
			// dtChild
			// 
			this.dtChild.Columns.AddRange(new System.Data.DataColumn[] {
																		   this.ChildID,
																		   this.MasterLink,
																		   this.ChildValue});
			this.dtChild.Constraints.AddRange(new System.Data.Constraint[] {
																			   new System.Data.ForeignKeyConstraint("MasterChild", "dtMaster", new string[] {
																																								"MasterID"}, new string[] {
																																															  "MasterLink"}, System.Data.AcceptRejectRule.None, System.Data.Rule.Cascade, System.Data.Rule.Cascade)});
			this.dtChild.TableName = "dtChild";
			// 
			// ChildID
			// 
			this.ChildID.AllowDBNull = false;
			this.ChildID.AutoIncrement = true;
			this.ChildID.Caption = "ChildID";
			this.ChildID.ColumnName = "ChildID";
			this.ChildID.DataType = typeof(int);
			// 
			// MasterLink
			// 
			this.MasterLink.AllowDBNull = false;
			this.MasterLink.Caption = "MasterLink";
			this.MasterLink.ColumnName = "MasterLink";
			this.MasterLink.DataType = typeof(int);
			// 
			// ChildValue
			// 
			this.ChildValue.Caption = "ChildValue";
			this.ChildValue.ColumnName = "ChildValue";
			// 
			// frmDataSets
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 373);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCopy,
																		  this.btnClone,
																		  this.btnRelation,
																		  this.btnTable,
																		  this.dgOrders,
																		  this.lbClients,
																		  this.lbEmployees,
																		  this.lblOrders,
																		  this.lblClients,
																		  this.lblEmployees});
			this.Name = "frmDataSets";
			this.Text = "DataSets";
			((System.ComponentModel.ISupportInitialize)(this.dgOrders)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dsMaster1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dsUntyped)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dtMaster)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dtChild)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmDataSets());
		}

		private void btnTable_Click(object sender, System.EventArgs e)
		{
			string strMessage;

			// Create the table
			System.Data.DataTable dtEmployees;
			dtEmployees = this.dsEmployees.Tables.Add("Employees");

			//Add the columns
			dtEmployees.Columns.Add("EmployeeID", Type.GetType("System.Int32"));
			dtEmployees.Columns.Add("FirstName", Type.GetType("System.String"));
			dtEmployees.Columns.Add("LastName", Type.GetType("System.String"));

			//Fill the dataset
			this.daEmployees.Fill(this.dsEmployees.Tables["Employees"]);

			strMessage = "The first employee is ";
			strMessage += this.dsEmployees.Tables["Employees"].Rows[0]["LastName"];
			MessageBox.Show(strMessage);

		}

		private void btnRelation_Click(object sender, System.EventArgs e)
		{
			string strMessage;

			//Add a new relation
			this.dsMaster1.Relations.Add("CustomerOrders",
				this.dsMaster1.CustomerList.CustomerIDColumn,
				this.dsMaster1.OrderTotals.CustomerIDColumn);

			strMessage = "The name of the DataRelation is ";
			strMessage+= this.dsMaster1.Relations[0].RelationName.ToString();
			MessageBox.Show(strMessage);

		}

		private void btnClone_Click(object sender, System.EventArgs e)
		{
			string strMessage;
			System.Data.DataSet dsClone;

			dsClone = this.dsMaster1.Clone();

			strMessage = "The cloned dataset has ";
			strMessage += dsClone.Tables.Count.ToString();
			strMessage += " tables.";
			MessageBox.Show(strMessage);

		}

		private void btnCopy_Click(object sender, System.EventArgs e)
		{
			string strMessage;
			System.Data.DataSet dsCopy;

			//Fill the original dataset
			this.daCustomers.Fill(this.dsMaster1.CustomerList);

			dsCopy = this.dsMaster1.Copy();
			strMessage = "The copied dataset has ";
			strMessage += dsCopy.Tables["CustomerList"].Rows.Count.ToString();
			strMessage += " rows in the CustomerList.";
			MessageBox.Show(strMessage);

		}
	}
}

⌨️ 快捷键说明

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