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

📄 form3.cs

📁 《学籍管理系统》 “c# WinForm 数据库操作的简单范例 (SQL Server 2000)”
💻 CS
📖 第 1 页 / 共 2 页
字号:
																																																					   new System.Data.Common.DataColumnMapping("password", "password"),
																																																					   new System.Data.Common.DataColumnMapping("popedom", "popedom")})});
			this.sqlDataAdapter1.UpdateCommand = this.sqlCommand2;
			// 
			// dataSet41
			// 
			this.dataSet41.DataSetName = "DataSet4";
			this.dataSet41.Locale = new System.Globalization.CultureInfo("zh-CN");
			// 
			// label6
			// 
			this.label6.ForeColor = System.Drawing.Color.Black;
			this.label6.Location = new System.Drawing.Point(8, 164);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(80, 16);
			this.label6.TabIndex = 3;
			this.label6.Text = "设置 权限:";
			// 
			// comboBox1
			// 
			this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox1.Items.AddRange(new object[] {
														   "超级用户  (拥有所有权限 包括修改用户)",
														   "管 理 员  (拥有除添加/删除/修改权限)",
														   "受限用户  (有查看/添加权限)",
														   "一般用户  (只有查看权限)"});
			this.comboBox1.Location = new System.Drawing.Point(96, 160);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(224, 20);
			this.comboBox1.TabIndex = 4;
			this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
			// 
			// Form3
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.Color.Teal;
			this.ClientSize = new System.Drawing.Size(380, 236);
			this.ControlBox = false;
			this.Controls.Add(this.comboBox1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.label6);
			this.MaximumSize = new System.Drawing.Size(388, 244);
			this.MinimumSize = new System.Drawing.Size(388, 244);
			this.Name = "Form3";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			((System.ComponentModel.ISupportInitialize)(this.dataSet41)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

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

		#region 实现方法
		// 添加用户
		private void Add_User()
		{
			try 
			{	
				dataSet41.Clear();			
				sqlConnection1.Open();								
				DataRow drAdd ;				 
				drAdd = dataSet41.Tables["Administrator"].NewRow();					
				drAdd[0] = textBox3.Text;
				drAdd[1] = textBox4.Text;
				drAdd[2] = popedom;
				dataSet41.Tables["Administrator"].Rows.Add(drAdd);
				sqlDataAdapter1.Update(dataSet41,"Administrator");
				sqlConnection1.Close();				
				MessageBox.Show("记录成功!");
			}
			catch(SqlException sqle)
			{
				sqlConnection1.Close();
				MessageBox.Show(sqle.Message);
			}
			catch(Exception e1)
			{
				sqlConnection1.Close();
				MessageBox.Show(e1.Message);
			}
		}

		//删除用户
		private void Delete_User()
		{
			dataSet41.Clear();
			if(MessageBox.Show("删除后数据将无法恢复\n确定删除?","删除确认",MessageBoxButtons.YesNo) == DialogResult.Yes)
			{
				try
				{
					sqlConnection1.Open();						
					string deleteCommand1 = "DELETE  from Administrator where name='"+ textBox3.Text +"'";
					sqlCommand1 = new SqlCommand(deleteCommand1,sqlConnection1);					
					sqlCommand1.ExecuteNonQuery();					
					sqlDataAdapter1.Update(dataSet41,"Administrator");					
					MessageBox.Show("资料已删除!");
					sqlConnection1.Close();
				}
				catch(SqlException sqle)
				{
					MessageBox.Show(sqle.Message);
					sqlConnection1.Close();
				}
				catch(Exception e1)
				{
					MessageBox.Show(e1.Message);
					sqlConnection1.Close();
				}
			}
		}
		//修改用户
		private void Revise_User()
		{
			try
			{					
				string upDateString;				
				sqlConnection1.Open();
				upDateString = "UPDATE Administrator SET password='"+ textBox3.Text +"',popedom='"+popedom+"' where name ='"+ textBox3.Text +"'";
				sqlCommand2 = new SqlCommand(upDateString,sqlConnection1);				
				sqlCommand2.ExecuteNonQuery();								
				sqlConnection1.Close();
				MessageBox.Show("修改成功!");				
			}
			catch(SqlException sqle)
			{
				sqlConnection1.Close();
				MessageBox.Show(sqle.Message);				
			}
			catch(Exception e1)
			{
				sqlConnection1.Close();
				MessageBox.Show(e1.Message);				
			}
			finally
			{
				
			}
		}

		//检查管理员信息
		private void Check_Administrator()
		{
			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))
							{								
								if("a" == myReader.GetSqlString(2).ToString())
								{
									check = true;
								}
								else
								{
									MessageBox.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);
			}
		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			Check_Administrator();
			if(check)
			{
				Add_User();
				check = false;
			}			
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			Check_Administrator();
			if(check)
			{
				Revise_User();
				check = false;
			}
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			Check_Administrator();
			if(check)
			{
				Delete_User();
				check = false;
			}
		}

		private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			switch(comboBox1.SelectedIndex)
			{
				case 0 : popedom = "a";break;
				case 1 : popedom = "b";break;
				case 2 : popedom = "c";break;
				case 3 : popedom = "d";break;
				default: popedom = "d";break;
			}
		}
	}
}

⌨️ 快捷键说明

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