C# 单击某个按钮时弹出拒绝访问消息,具体取决于哪个用户已登录
在登录表单中,当我以医生表中的Jack身份登录时,它将转到第二页。如果单击护士按钮1或护士按钮2,我想弹出一条拒绝访问消息,因为杰克不是护士,而是医生。相反,如果我以护士表中的Mary登录,它将进入第二页。我想在单击医生按钮1或医生按钮2时弹出一条拒绝访问消息,因为玛丽不是医生,而是护士 第二页的按钮名称为BTNDotor1、BTNDotor2、btnNurse1和btnNurse2C# 单击某个按钮时弹出拒绝访问消息,具体取决于哪个用户已登录,c#,windows,forms,button,login,C#,Windows,Forms,Button,Login,在登录表单中,当我以医生表中的Jack身份登录时,它将转到第二页。如果单击护士按钮1或护士按钮2,我想弹出一条拒绝访问消息,因为杰克不是护士,而是医生。相反,如果我以护士表中的Mary登录,它将进入第二页。我想在单击医生按钮1或医生按钮2时弹出一条拒绝访问消息,因为玛丽不是医生,而是护士 第二页的按钮名称为BTNDotor1、BTNDotor2、btnNurse1和btnNurse2 **//login Form codes** using System; using Sys
**//login Form codes**
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace GRP_02_03_SACP
{
public partial class page_one : Form
{
public page_one()
{
InitializeComponent();
}
private void page_one_Load(object sender, EventArgs e)
{
}
private void btnLogin_Click(object sender, EventArgs e)
{
//retrieve connection information info from App.config
string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
//STEP 1: Create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//STEP 2: Create command
string strCommandtext = "SELECT dUsername, dPassword from DOCTOR";
// Add a WHERE Clause to SQL statement
strCommandtext += " WHERE dUsername=@dname AND dPassword=@dpwd;";
strCommandtext += "SELECT nUsername, nPassword from NURSE WHERE nUsername=@nname AND nPassword=@npwd;";
SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
cmd.Parameters.AddWithValue("@dname", textUsername.Text);
cmd.Parameters.AddWithValue("@dpwd", txtPassword.Text);
cmd.Parameters.AddWithValue("@nname", textUsername.Text);
cmd.Parameters.AddWithValue("@npwd", txtPassword.Text);
try
{
// STEP 3: open connection and retrieve data by calling ExecuteReader
myConnect.Open();
// STEP 4: Access Data
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) //For Doctor
{
if (MessageBox.Show("Login Successful") == DialogResult.OK)
{
page_two form = new page_two();
form.Show();
return;
}
}
reader.NextResult();
while (reader.Read()) //For Nurse
{
if (MessageBox.Show("Login Successful") == DialogResult.OK)
{
page_two form = new page_two();
form.Show();
return;
}
}
//STEP 5: close connection
reader.Close();
MessageBox.Show("Invalid username or password");
}
catch (SqlException ex)
{
}
finally
{
//STEP 5: close connection
myConnect.Close();
}
}
}
}
//第二页代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GRP_02_03_SACP
{
public partial class page_two : Form
{
public page_two()
{
InitializeComponent();
}
private void btnDoctor1_Click(object sender, EventArgs e)
{
}
private void btnDoctor2_Click(object sender, EventArgs e)
{
}
private void btnNurse1_Click(object sender, EventArgs e)
{
}
private void btnNurse2_Click(object sender, EventArgs e)
{
}
}
}
将此添加到您的代码中:
public Int JobPosition;
在这里,您将定义每个位置的位置
将btnLogin\u上的代码单击更改为:
private void btnLogin_Click(object sender, EventArgs e)
{
//retrieve connection information info from App.config
string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
//STEP 1: Create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//STEP 2: Create command
string strCommandtext = "SELECT dUsername, dPassword from DOCTOR";
// Add a WHERE Clause to SQL statement
strCommandtext += " WHERE dUsername=@dname AND dPassword=@dpwd;";
strCommandtext += "SELECT nUsername, nPassword from NURSE WHERE nUsername=@nname AND nPassword=@npwd;";
SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
cmd.Parameters.AddWithValue("@dname", textUsername.Text);
cmd.Parameters.AddWithValue("@dpwd", txtPassword.Text);
cmd.Parameters.AddWithValue("@nname", textUsername.Text);
cmd.Parameters.AddWithValue("@npwd", txtPassword.Text);
try
{
// STEP 3: open connection and retrieve data by calling ExecuteReader
myConnect.Open();
// STEP 4: Access Data
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) //For Doctor
{
if (MessageBox.Show("Login Successful") == DialogResult.OK)
{
JobPosition = 1; //Doctor
page_two form = new page_two(JobPosition);
form.Show();
return;
}
}
reader.NextResult();
while (reader.Read()) //For Nurse
{
if (MessageBox.Show("Login Successful") == DialogResult.OK)
{
JobPosition = 2; //Nurse
page_two form = new page_two(JobPosition);
form.Show();
return;
}
}
//STEP 5: close connection
reader.Close();
MessageBox.Show("Invalid username or password");
}
catch (SqlException ex)
{
}
finally
{
//STEP 5: close connection
myConnect.Close();
}
}
在第二页的code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GRP_02_03_SACP
{
public partial class page_two : Form
{
private Int JopPosition;
public page_two()
{
InitializeComponent();
}
public page_two(Int _Position)
{
InitializeComponent();
JopPosition = _Position;
}
private void btnDoctor1_Click(object sender, EventArgs e)
{
}
private void btnDoctor2_Click(object sender, EventArgs e)
{
}
private void btnNurse1_Click(object sender, EventArgs e)
{
if (JopPosition == 1)
{
MessageBox.Show("access denied");
}
}
private void btnNurse2_Click(object sender, EventArgs e)
{
}
}
}虽然这不是最好的解决方案,但如果它是一个小应用程序,而不是复杂的代码,它就可以工作。