C#初学者-如何使用指定帐户检索数据
本文关键字:检索 数据 初学者 何使用 | 更新日期: 2023-09-27 18:29:31
HI~我有一个关于如何从特定帐户检索数据的问题。。例如,数据库上存在have4错误报告
2个错误报告被分配给开发人员a,另外2个缺陷报告被分配到开发人员b。
如果我以开发人员a的身份登录,我只能查看分配给我的2个bug。如何设置代码?
(对于许多答案,我指出我的编码有SQL注入的风险,我会在完成任务后编辑它。:)
这是我的编码:
private void bug_view_Click(object sender, EventArgs e)
{
dataGridView1.Visible = true;
bug_info_panel.Visible = true;
string constring = "datasource=localhost;username=root;password=";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand("select * from bug.bug where Assigned = '';", conDataBase);
try
{
dataGridView1.Visible = true;
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我的登录表单编码enter code here
:
try
{
bool IsAdminUser = false;
bool IsDeveloper = false;
string myConnection = "datasource=localhost;username=root;password=";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand SelectCommand = new MySqlCommand("select * from logintable.account where id='" + this.username_txt.Text + "' and password='" + this.password_txt.Text + "' ;", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
IsAdminUser = myReader["permissions"].ToString().Equals("Admin");
IsDeveloper = myReader["permissions"].ToString().Equals("Developer");
}
if (count == 1 && IsDeveloper == true)
{
MessageBox.Show("You are logged in as Developer ");
this.Hide();
DeveloperForm developform = new DeveloperForm();
developform.ShowDialog();
}
else if (count == 1 && IsAdminUser == true)
{
MessageBox.Show("You are logged in as administrator ");
this.Hide();
AdminForm adminForm = new AdminForm();
adminForm.ShowDialog();
}
else if (count == 1)
{
MessageBox.Show("You are logged in");
this.Hide();
UserForm userform = new UserForm();
userform.ShowDialog();
}
else
MessageBox.Show("Username or Password is not correct ..Please try again");
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我可以看到您没有使用推荐的身份验证方法。无论如何,您可以通过使用Application/Session变量来实现您想要的内容。用户登录后,创建一个Application/Session变量,并将用户Id存储在其中,如下所示:
Application["user_id"]=myReader["Id"];
或
Session["user_id"]=myReader["Id"];
然后你可以使用这个值来获取像一样的Bug报告
MySqlCommand cmdDataBase = new MySqlCommand("select * from bug.bug where Assigned = @user;", conDataBase);
cmdDataBase.Parameters.AddWithValue("@user",Application["user_id"])
希望这能帮助