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

📄 form1.cs

📁 SQL Server 数据库开发经典案例解析
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;


namespace AddDelete {
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form {
		private SqlDataAdapter	dataAdapter;
		private	DataSet			myDataSet = new DataSet();

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button add;
		private System.Windows.Forms.Button delete;
		private System.Windows.Forms.Button accept;
		private System.Windows.Forms.Button reject;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button reload;
		private System.Windows.Forms.Button update;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1() {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// Read the shipper table in the Northwind databse
			OpenShipperTable();

			// Bind the datagrid to the shipper table in the dataset
			dataGrid1.DataSource = myDataSet.Tables["Shippers"];
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			this.label1 = new System.Windows.Forms.Label();
			this.add = new System.Windows.Forms.Button();
			this.delete = new System.Windows.Forms.Button();
			this.accept = new System.Windows.Forms.Button();
			this.reject = new System.Windows.Forms.Button();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.reload = new System.Windows.Forms.Button();
			this.update = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(224, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Data: Northwest Shipping, 555-1212";
			// 
			// add
			// 
			this.add.Location = new System.Drawing.Point(16, 48);
			this.add.Name = "add";
			this.add.TabIndex = 1;
			this.add.Text = "Add";
			this.add.Click += new System.EventHandler(this.add_Click);
			// 
			// delete
			// 
			this.delete.Location = new System.Drawing.Point(16, 80);
			this.delete.Name = "delete";
			this.delete.TabIndex = 2;
			this.delete.Text = "Delete";
			this.delete.Click += new System.EventHandler(this.delete_Click);
			// 
			// accept
			// 
			this.accept.Location = new System.Drawing.Point(248, 88);
			this.accept.Name = "accept";
			this.accept.TabIndex = 3;
			this.accept.Text = "Accept";
			this.accept.Click += new System.EventHandler(this.accept_Click);
			// 
			// reject
			// 
			this.reject.Location = new System.Drawing.Point(328, 88);
			this.reject.Name = "reject";
			this.reject.TabIndex = 4;
			this.reject.Text = "Reject";
			this.reject.Click += new System.EventHandler(this.reject_Click);
			// 
			// dataGrid1
			// 
			this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(16, 120);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.Size = new System.Drawing.Size(392, 160);
			this.dataGrid1.TabIndex = 5;
			// 
			// reload
			// 
			this.reload.Location = new System.Drawing.Point(240, 16);
			this.reload.Name = "reload";
			this.reload.Size = new System.Drawing.Size(164, 23);
			this.reload.TabIndex = 7;
			this.reload.Text = "Reload from Database";
			this.reload.Click += new System.EventHandler(this.reload_Click);
			// 
			// update
			// 
			this.update.Location = new System.Drawing.Point(328, 56);
			this.update.Name = "update";
			this.update.TabIndex = 8;
			this.update.Text = "Update";
			this.update.Click += new System.EventHandler(this.update_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(416, 294);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.update,
																		  this.reload,
																		  this.dataGrid1,
																		  this.reject,
																		  this.accept,
																		  this.delete,
																		  this.add,
																		  this.label1});
			this.Name = "Form1";
			this.Text = "ADO.NET Example";
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

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

		// Reads the Shipper table, Northwind database
		private void OpenShipperTable() {
			// local Northwind database, Windows security
			string connectionString = @"data source=localhost; Initial catalog=Northwind; integrated security=SSPI";

			// Read all data from the Shippers table
			string commandString = @"SELECT * FROM Shippers";

			// Create the data adapter
			dataAdapter = new SqlDataAdapter( commandString, connectionString );

			// Build the Insert, Update and Delete commands
			SqlCommandBuilder scb = new SqlCommandBuilder( dataAdapter );

			// Fill a dataset table
			dataAdapter.Fill( myDataSet, "Shippers" );
		}

		// Called when Add is clicked
		// Adds a row of data to the table in the dataset
		private void add_Click(object sender, System.EventArgs e) {
			DataTable sTable = myDataSet.Tables["Shippers"];
			object[] o = { 0, "Northwest Shipping", "555-1212" };
			sTable.Rows.Add( o );
		}


		// Called when Delete is clicked
		// Deletes the fourth row from the shippers table in the dataset
		private void delete_Click(object sender, System.EventArgs e) {
			DataTable sTable = myDataSet.Tables["Shippers"];
			if ( sTable.Rows.Count > 3 ) {
				sTable.Rows[3].Delete();
			}
			else {
				MessageBox.Show(this, "Fourth row does not exist", "Cannot Perform Operation");
			}
		}

		// Called when Reload from Database is clicked
		// Removes the current dataset and reloads the Shippers table from the Northwind database
		private void reload_Click(object sender, System.EventArgs e) {
			myDataSet.Clear();
			OpenShipperTable();
		}

		// Called when Accept is clicked
		// Accepts the changes made to the dataset
		private void accept_Click(object sender, System.EventArgs e) {
			myDataSet.AcceptChanges();
		}
		
		// Called when Reject is clicked
		// Rejects the changes made to the dataset
		private void reject_Click(object sender, System.EventArgs e) {
			myDataSet.RejectChanges();
		}

		// Called when Update is clicked
		// Applies the dataset changes to the data base
		private void update_Click(object sender, System.EventArgs e) {
			dataAdapter.Update(myDataSet, "Shippers");
		}
	}
}

⌨️ 快捷键说明

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