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

📄 form2.cs

📁 《学籍管理系统》 “c# WinForm 数据库操作的简单范例 (SQL Server 2000)”
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Text;

namespace StudentManage
{
	/// <summary>
	/// Form3 的摘要说明。
	/// </summary>
	public class Form2 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
		private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
		private System.Data.SqlClient.SqlConnection sqlConnection1;
		private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
		private StudentManage.DataSet2 dataSet21;
		private string password = null;
		private int secretkey = 0;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private string ID,userID;

		//申明INI文件的读操作函数GetPrivateProfileString()
		[ DllImport ( "kernel32" ) ] 
		private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal , int size , string filePath ) ;


		public Form2()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		static void Main() 
		{
			Application.Run(new Form2());
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form2));
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
			this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
			this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
			this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
			this.dataSet21 = new StudentManage.DataSet2();
			((System.ComponentModel.ISupportInitialize)(this.dataSet21)).BeginInit();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button1.ForeColor = System.Drawing.Color.Lime;
			this.button1.Location = new System.Drawing.Point(72, 84);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(80, 24);
			this.button1.TabIndex = 2;
			this.button1.Text = "确  定";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button2.ForeColor = System.Drawing.Color.Lime;
			this.button2.Location = new System.Drawing.Point(184, 84);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(80, 24);
			this.button2.TabIndex = 3;
			this.button2.Text = "取  消";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(72, 8);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(192, 21);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(72, 48);
			this.textBox2.Name = "textBox2";
			this.textBox2.PasswordChar = '*';
			this.textBox2.Size = new System.Drawing.Size(192, 21);
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "";
			this.textBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox2_KeyDown);
			// 
			// label1
			// 
			this.label1.ForeColor = System.Drawing.Color.Lime;
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 2;
			this.label1.Text = "用户名:";
			// 
			// label2
			// 
			this.label2.ForeColor = System.Drawing.Color.Lime;
			this.label2.Location = new System.Drawing.Point(8, 52);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "密  码:";
			// 
			// sqlSelectCommand1
			// 
			this.sqlSelectCommand1.CommandText = "SELECT name, password, popedom FROM Administrator";
			this.sqlSelectCommand1.Connection = this.sqlConnection1;
			// 
			// sqlConnection1
			// 
			this.sqlConnection1.ConnectionString = "workstation id=CALMHAWK;packet size=4096;user id=sa;password=calmhawk;data source" +
				"=calmhawk;persist security info=False;initial catalog=Students";
			// 
			// sqlInsertCommand1
			// 
			this.sqlInsertCommand1.CommandText = "INSERT INTO Administrator(name, password, popedom) VALUES (@name, @password, @pop" +
				"edom); SELECT name, password, popedom FROM Administrator";
			this.sqlInsertCommand1.Connection = this.sqlConnection1;
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.NVarChar, 50, "name"));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@password", System.Data.SqlDbType.NVarChar, 50, "password"));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@popedom", System.Data.SqlDbType.NVarChar, 2, "popedom"));
			// 
			// sqlDataAdapter1
			// 
			this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
			this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
			this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																									  new System.Data.Common.DataTableMapping("Table", "Administrator", new System.Data.Common.DataColumnMapping[] {
																																																					   new System.Data.Common.DataColumnMapping("name", "name"),
																																																					   new System.Data.Common.DataColumnMapping("password", "password"),
																																																					   new System.Data.Common.DataColumnMapping("popedom", "popedom")})});
			// 
			// dataSet21
			// 
			this.dataSet21.DataSetName = "DataSet2";
			this.dataSet21.Locale = new System.Globalization.CultureInfo("zh-CN");
			// 
			// Form2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.Color.Teal;
			this.ClientSize = new System.Drawing.Size(296, 125);
			this.ControlBox = false;
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label2);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximumSize = new System.Drawing.Size(304, 152);
			this.MinimumSize = new System.Drawing.Size(304, 152);
			this.Name = "Form2";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "学籍管理系统V1.1登陆";
			this.Load += new System.EventHandler(this.Form2_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataSet21)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			check_user();
		}

		//检查用户
		private void check_user()
		{
			// 查询
			if(textBox1.Text != "")
			{
				try
				{
					sqlConnection1.Open();
					string sql;
					if(textBox1.Text != "")
					{
						sql="select * from Administrator where name='"+ textBox1.Text +"'";
						SqlCommand myCommand = new SqlCommand(sql,sqlConnection1);
						SqlDataReader myReader = myCommand.ExecuteReader();
						try
						{
							if (myReader.Read())
							{						
								if(textBox2.Text == myReader.GetSqlString(1))
								{
									Form1 f = new Form1();
									string popedom;
									popedom = myReader.GetSqlString(2).ToString();
									switch(popedom)
									{
										case "a" : ; break;
										case "b" : f.menuItem2.Enabled = false;break;
										case "c" : f.menuItem2.Enabled = false;f.menuItem15.Enabled = false;f.menuItem16.Enabled = false;f.button3.Enabled = false;f.button4.Enabled = false;f.button5.Enabled = false;break;
										case "d" : f.menuItem14.Enabled = false;f.menuItem2.Enabled = false;f.menuItem15.Enabled = false;f.menuItem16.Enabled = false;f.button3.Enabled = false;f.button4.Enabled = false;f.button5.Enabled = false;break;
										default :f.menuItem15.Enabled = false;f.menuItem16.Enabled = false;f.menuItem14.Enabled = false;f.menuItem2.Enabled = false; f.button3.Enabled = false;f.button4.Enabled = false;f.button5.Enabled = false; break;
									}
									f.textBox16.Text = this.textBox1.Text;
									textBox2.Text = null;
									this.Hide();
									f.Show();
								}
								else
								{
									MessageBox.Show("密码错误!");
								}
								myReader.Close();
								sqlConnection1.Close();											
							}
							else
							{
								MessageBox.Show("此用户不存在!");
							}
						}

						finally 
						{
							myReader.Close();
							sqlConnection1.Close();
						}
					}
					else 
					{				
						sqlConnection1.Close();
					}
				}
				catch(SqlException sqle)
				{
					MessageBox.Show(sqle.Message);
				}
			}
			else
			{
				MessageBox.Show("请输入用户名!");
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			Form1 f1 = new Form1();
			f1.groupBox1.Enabled = false;
			f1.button1.Enabled = false;
			f1.button2.Enabled = false;
			f1.button3.Enabled = false;
			f1.button4.Enabled = false;
			f1.button5.Enabled = false;
			f1.comboBox1.Enabled = false;
			f1.groupBox1.Enabled = false;
			f1.menuItem2.Enabled = false;
			f1.menuItem21.Enabled = false;
			f1.menuItem35.Enabled = false;
			f1.menuItem9.Enabled = false;
			f1.textBox1.Enabled = false;
			f1.textBox16.Enabled = false;
			this.Hide();
			f1.Show();
		}

		private void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				check_user();
			}
		}

		//读取服务器信息
		private void Read_SystemInfo()
		{
			try
			{
				//获取配置信息
				string s =  Application.ExecutablePath;
				string s1;
				s1 = s.Replace("StudentManage.exe","config.ini");
				StringBuilder temp1 = new StringBuilder ( 255 ) ; 
				int i1 = GetPrivateProfileString ("Calmhawk" ,"Sever" , "" , temp1 , 255 , s1 ) ; 
			
				ID = temp1.ToString();
				//      
				StringBuilder temp2 = new StringBuilder ( 255 ) ; 
				int i2 = GetPrivateProfileString ("Calmhawk" ,"userID" , "" , temp2 , 255 , s1 ) ;
				userID = temp2.ToString();
				StringBuilder temp3 = new StringBuilder ( 255 ) ; 
				int i3 = GetPrivateProfileString ("Calmhawk" ,"password" , "" , temp3 , 255 , s1 ) ;
				password = temp3.ToString();
				StringBuilder temp4 = new StringBuilder ( 255 ) ; 
				int i4 = GetPrivateProfileString ("Calmhawk" ,"secretkey" , "" , temp4 , 255 , s1 ) ;
				secretkey = int.Parse(temp4.ToString());
				Calmhawk_Lab.Password.String_Loose(ref password , secretkey);
				this.sqlConnection1.ConnectionString = "workstation id='"+ID+"';packet size=4096;user id='"+userID+"';password='"+password+"';data source=ca" +
					"lmhawk;persist security info=False;initial catalog=Students";
			}
			catch(Exception e1)
			{
				MessageBox.Show("请先配置服务器信息!");
			}
		}

		private void Form2_Load(object sender, System.EventArgs e)
		{
			Read_SystemInfo();
		}
	}
}

⌨️ 快捷键说明

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