www.pudn.com > MainPrj_CarShop.rar > frmLogin.cs


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using System.Data; 
using System.Data.SqlClient; 
 
namespace MainPrj_CarShop 
{ 
	///  
	/// frmLogin 的摘要说明。 
	///  
	public class frmLogin : System.Windows.Forms.Form 
	{ 
		private System.Windows.Forms.Label lbluer; 
		private System.Windows.Forms.ToolTip toolTip1; 
		private System.Windows.Forms.Label label1; 
		private System.Windows.Forms.Button btnConfirm; 
		private System.Windows.Forms.Button btnCancel; 
		private System.Windows.Forms.PictureBox pictureBox1; 
		private System.ComponentModel.IContainer components; 
 
		private System.Data.SqlClient.SqlConnection conLogin; 
		private System.Windows.Forms.ComboBox cboName; 
		private System.Windows.Forms.TextBox txtPwd; 
		private System.Windows.Forms.ContextMenu contextMenu1; 
		private System.Data.SqlClient.SqlCommand cmdLogin; 
 
		public frmLogin() 
		{ 
			// 
			// Windows 窗体设计器支持所必需的 
			// 
			InitializeComponent(); 
 
			// 
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
			// 
		} 
 
		///  
		/// 清理所有正在使用的资源。 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			try 
			{ 
				if( disposing ) 
				{ 
					if(components != null) 
					{ 
						components.Dispose(); 
					} 
				} 
				base.Dispose( disposing ); 
			} 
			catch(Exception ex) 
			{ 
				MessageBox.Show("系统发生一个异常的错误!请与管理员联系。\n"+ex.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error); 
			} 
			 
		} 
 
		#region Windows 窗体设计器生成的代码 
		///  
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改 
		/// 此方法的内容。 
		///  
		private void InitializeComponent() 
		{ 
			this.components = new System.ComponentModel.Container(); 
			this.lbluer = new System.Windows.Forms.Label(); 
			this.cboName = new System.Windows.Forms.ComboBox(); 
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); 
			this.label1 = new System.Windows.Forms.Label(); 
			this.txtPwd = new System.Windows.Forms.TextBox(); 
			this.btnConfirm = new System.Windows.Forms.Button(); 
			this.btnCancel = new System.Windows.Forms.Button(); 
			this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
			this.contextMenu1 = new System.Windows.Forms.ContextMenu(); 
			this.SuspendLayout(); 
			//  
			// lbluer 
			//  
			this.lbluer.AutoSize = true; 
			this.lbluer.Location = new System.Drawing.Point(88, 91); 
			this.lbluer.Name = "lbluer"; 
			this.lbluer.Size = new System.Drawing.Size(54, 17); 
			this.lbluer.TabIndex = 0; 
			this.lbluer.Text = "用户名称"; 
			//  
			// cboName 
			//  
			this.cboName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 
			this.cboName.Location = new System.Drawing.Point(144, 88); 
			this.cboName.Name = "cboName"; 
			this.cboName.Size = new System.Drawing.Size(128, 20); 
			this.cboName.TabIndex = 1; 
			//  
			// label1 
			//  
			this.label1.AutoSize = true; 
			this.label1.Location = new System.Drawing.Point(88, 124); 
			this.label1.Name = "label1"; 
			this.label1.Size = new System.Drawing.Size(54, 17); 
			this.label1.TabIndex = 0; 
			this.label1.Text = "用户密码"; 
			//  
			// txtPwd 
			//  
			this.txtPwd.Location = new System.Drawing.Point(144, 120); 
			this.txtPwd.Name = "txtPwd"; 
			this.txtPwd.PasswordChar = '*'; 
			this.txtPwd.Size = new System.Drawing.Size(128, 21); 
			this.txtPwd.TabIndex = 3; 
			this.txtPwd.Text = "123456"; 
			//  
			// btnConfirm 
			//  
			this.btnConfirm.DialogResult = System.Windows.Forms.DialogResult.OK; 
			this.btnConfirm.Location = new System.Drawing.Point(92, 152); 
			this.btnConfirm.Name = "btnConfirm"; 
			this.btnConfirm.Size = new System.Drawing.Size(64, 24); 
			this.btnConfirm.TabIndex = 4; 
			this.btnConfirm.Text = "确定(&C)"; 
			this.btnConfirm.Click += new System.EventHandler(this.btnConfirm_Click); 
			//  
			// btnCancel 
			//  
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; 
			this.btnCancel.Location = new System.Drawing.Point(204, 152); 
			this.btnCancel.Name = "btnCancel"; 
			this.btnCancel.Size = new System.Drawing.Size(64, 24); 
			this.btnCancel.TabIndex = 4; 
			this.btnCancel.Text = "取消(&E)"; 
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); 
			//  
			// pictureBox1 
			//  
			this.pictureBox1.BackColor = System.Drawing.Color.LightGoldenrodYellow; 
			this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top; 
			this.pictureBox1.Location = new System.Drawing.Point(0, 0); 
			this.pictureBox1.Name = "pictureBox1"; 
			this.pictureBox1.Size = new System.Drawing.Size(368, 80); 
			this.pictureBox1.TabIndex = 5; 
			this.pictureBox1.TabStop = false; 
			//  
			// frmLogin 
			//  
			this.AcceptButton = this.btnConfirm; 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
			this.CancelButton = this.btnCancel; 
			this.ClientSize = new System.Drawing.Size(368, 206); 
			this.Controls.Add(this.pictureBox1); 
			this.Controls.Add(this.btnConfirm); 
			this.Controls.Add(this.txtPwd); 
			this.Controls.Add(this.cboName); 
			this.Controls.Add(this.lbluer); 
			this.Controls.Add(this.label1); 
			this.Controls.Add(this.btnCancel); 
			this.MaximizeBox = false; 
			this.MinimizeBox = false; 
			this.Name = "frmLogin"; 
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
			this.Text = "登 陆"; 
			this.Closing += new System.ComponentModel.CancelEventHandler(this.frmLogin_Closing); 
			this.Load += new System.EventHandler(this.frmLogin_Load); 
			this.ResumeLayout(false); 
 
		} 
		#endregion    
 
		private void frmLogin_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
		{ 
			 
		} 
 
		private	ArrayList li=new ArrayList(); 
		private void frmLogin_Load(object sender, System.EventArgs e) 
		{ 
			   
			try 
			{ 
				this.conLogin=new System.Data.SqlClient.SqlConnection("server=.;uid=sa;pwd=;database=dbCarsys"); 
				this.conLogin.Open(); 
				this.cmdLogin=new System.Data.SqlClient.SqlCommand("select uName,uCode from tblUser",this.conLogin); 
				SqlDataReader dr=this.cmdLogin.ExecuteReader(); 
				while(dr.Read()) 
				{ 
					li.Add( new User(dr.GetString(1),dr.GetString(0))); 
				} 
				dr.Close(); 
				this.cboName.DataSource=li; 
				this.cboName.DisplayMember="Name"; 
				this.cboName.ValueMember="Code"; 
			} 
			catch(SqlException ex) 
			{ 
				MessageBox.Show("系统发生一个异常的错误!请与管理员联系。\n"+ex.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);; 
			} 
			catch(Exception ex) 
			{ 
				MessageBox.Show("数据库操作发生一个异常的错误!请与管理员联系。\n"+ex.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error); 
			} 
			finally 
			{ 
				this.conLogin.Close(); 
			} 
		} 
 
		private void btnConfirm_Click(object sender, System.EventArgs e) 
		{ 
			if(this.cboName.SelectedItem==null) 
			{ 
				return; 
			} 
			else 
			{ 
				try 
				{ 
					this.conLogin=new System.Data.SqlClient.SqlConnection("server=.;uid=sa;pwd=;database=dbCarsys"); 
					this.conLogin.Open(); 
					string str="select uJob from tblUser where uCode='"+(string)this.cboName.SelectedValue+"' and uPwd='"+this.txtPwd.Text+"'"; 
					this.cmdLogin=new System.Data.SqlClient.SqlCommand(str,this.conLogin); 
					string job=(string)this.cmdLogin.ExecuteScalar(); 
					if(job==null) 
					{ 
						MessageBox.Show("登陆失败!","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning); 
						this.DialogResult=DialogResult.Cancel; 
						return; 
					} 
					 
//					MdiFrm_Main main=new MdiFrm_Main(this); 
					MdiFrm_Main.islog=true; 
					MdiFrm_Main.UserID=(string)this.cboName.SelectedValue; 
					MdiFrm_Main.UserName=this.cboName.Text; 
					MdiFrm_Main.UserJob=job; 
//					main.Show(); 
					this.Close(); 
					this.DialogResult=DialogResult.OK; 
				} 
				catch(SqlException ex) 
				{ 
					MessageBox.Show("数据库操作发生一个异常的错误!请与管理员联系。\n"+ex.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error); 
				} 
				catch(Exception ex) 
				{ 
					MessageBox.Show("系统发生一个异常的错误!请与管理员联系。\n"+ex.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error); 
				} 
				finally 
				{ 
					this.conLogin.Close(); 
					isExit=false; 
				} 
			} 
		} 
		bool isExit=false; 
		private void btnCancel_Click(object sender, System.EventArgs e) 
		{ 
			MdiFrm_Main.islog=false; 
			this.Close(); 
			this.DialogResult=DialogResult.Cancel; 
//			isExit=true; 
//			this.Close(); 
			//Application.Exit(); 
		} 
		 
		private class User 
		{ 
			 string code; 
			 string name; 
 
			public User(string code,string name) 
			{ 
				this.code=code; 
				this.name=name; 
			} 
 
			public string Code 
			{ 
				get 
				{ 
					return this.code; 
				} 
			} 
 
			public string Name 
			{ 
				get 
				{ 
					return this.name; 
				} 
			} 
		} 
	} 
	 
}