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

📄 form1.cs

📁 c#标准教程适合与处于不同学习阶段的人
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using T = System.Diagnostics.Trace;
using SD=System.Data;              // general DB classes
using SQL=System.Data.SqlClient;   // Microsoft SQL Server databases
using SWF=System.Windows.Forms;

namespace BankCustomerApp
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem mnuFile;
		private System.Windows.Forms.MenuItem mnuFileSave;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem mnuFileExit;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

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

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.mnuFile = new System.Windows.Forms.MenuItem();
			this.mnuFileSave = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.mnuFileExit = new System.Windows.Forms.MenuItem();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(24, 16);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(616, 328);
			this.dataGrid1.TabIndex = 0;
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mnuFile});
			// 
			// mnuFile
			// 
			this.mnuFile.Index = 0;
			this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuFileSave,
																					this.menuItem3,
																					this.mnuFileExit});
			this.mnuFile.Text = "&File";
			// 
			// mnuFileSave
			// 
			this.mnuFileSave.Index = 0;
			this.mnuFileSave.Text = "&Save";
			this.mnuFileSave.Click += new System.EventHandler(this.mnuFileSave_Click);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 1;
			this.menuItem3.Text = "-";
			// 
			// mnuFileExit
			// 
			this.mnuFileExit.Index = 2;
			this.mnuFileExit.Text = "E&xit";
			this.mnuFileExit.Click += new System.EventHandler(this.mnuFileExit_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(10, 22);
			this.ClientSize = new System.Drawing.Size(662, 367);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.dataGrid1});
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.MaximizeBox = false;
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "Bank Customer App";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void Form1_Load(object sender, System.EventArgs e)
		{
			SQL.SqlConnection  dbConn = null;
			SD.DataSet         ds     = null;

			try
			{
				string  sConnect;
				sConnect = string.Format("Server={0};Database={1};{2}",
					"localhost", 
					"Sales",
					"Integrated Security=SSPI");  // id=sa;pwd=????				dbConn = new SQL.SqlConnection(sConnect);

				string  sql;
				sql = "Select * From Customers Order By LastName Asc, FirstName Asc;";

				SQL.SqlCommand  dbCmd;
				dbCmd = new SQL.SqlCommand(sql, dbConn);

				SQL.SqlDataAdapter  dbAdapter;
				dbAdapter = new SQL.SqlDataAdapter(dbCmd);

				ds = new SD.DataSet();

				dbConn.Open();
				 dbAdapter.Fill(ds);
				dbConn.Close();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			finally 
			{
				try{ dbConn.Close(); }  catch{}
			}

			// bind customers to data grid for display
			this.dataGrid1.SetDataBinding(ds, "Table");
		}

		private void mnuFileExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void mnuFileSave_Click(object sender, System.EventArgs e)
		{
			SQL.SqlConnection  dbConn = null;
			SD.DataSet         ds     = null;

			try
			{
				string  sConnect;
				sConnect = string.Format("Server={0};Database={1};{2}",
					"localhost", 
					"Sales",
					"Integrated Security=SSPI");  // 搖id=sa;pwd=????				dbConn = new SQL.SqlConnection(sConnect);

				string  sql;
				sql = "Select * From Customers Order By LastName Asc, FirstName Asc;";

				SQL.SqlCommand  dbCmd;
				dbCmd = new SQL.SqlCommand(sql, dbConn);

				SQL.SqlDataAdapter  dbAdapter;
				dbAdapter = new SQL.SqlDataAdapter(dbCmd);

				SQL.SqlCommandBuilder  dbCmdBuilder;
				dbCmdBuilder = new SQL.SqlCommandBuilder(dbAdapter);

				ds = (SD.DataSet) this.dataGrid1.DataSource;

				dbConn.Open();
				 dbAdapter.Update(ds);
				dbConn.Close();

				MessageBox.Show("Changes saved!");
			}
			catch(Exception ex)
			{
				throw ex;
			}
			finally 
			{
				try{ dbConn.Close(); }  
				catch{}
			}
		}

	}//class
}//namespace

⌨️ 快捷键说明

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