我想在特定用户连接时显示特定数据。如果是管理员,我想检索所有数据,包含来自DB的NULL值。当操作员连接时,我想要检索除“ReportingOperator = NULL”之外的所有数据。
这是我的代码,显示datagridview的页面:
public partial class Repair : Form
{
public Repair()
{
InitializeComponent();
Main ss = new Main();
SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
if(ss.label3.Text == "admin")
{
maConnexion.Open();
SqlCommand command = maConnexion.CreateCommand();
command.CommandText = "SELECT * FROM FailOnly.";
SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
maConnexion.Close();
}
else
{
maConnexion.Open();
SqlCommand command = maConnexion.CreateCommand();
command.CommandText = "SELECT * FROM FailOnly WHERE ReportingOperator != NULL";
SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
maConnexion.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
/** SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
maConnexion.Open();
string Var1 = textBox1.Text;
SqlCommand command = maConnexion.CreateCommand();
command.Parameters.AddWithValue("@BoardName", Var1);
command.Parameters.AddWithValue("@Machine", Var1);
command.Parameters.AddWithValue("@SerialNum", Var1);
command.Parameters.AddWithValue("@FComponent", Var1);
command.CommandText = "SELECT * FROM FailOnly WHERE BoardName=@BoardName OR Machine=@Machine OR SerialNum=@SerialNum OR FComponent=@FComponent AND ReportingOperator != NULL";
SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];**/
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Main ff = new Main();
ff.Show();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
if (textBox1.Text != "")
{
maConnexion.Open();
string Var1 = textBox1.Text;
SqlCommand command = maConnexion.CreateCommand();
command.Parameters.AddWithValue("@BoardName", Var1 + "%");
command.Parameters.AddWithValue("@Machine", Var1 + "%");
command.Parameters.AddWithValue("@SerialNum", Var1 + "%");
command.Parameters.AddWithValue("@FComponent", Var1 + "%");
command.CommandText = "SELECT * FROM FailOnly WHERE BoardName LIKE @BoardName OR Machine LIKE @Machine OR SerialNum LIKE @SerialNum OR FComponent LIKE @FComponent";
SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
maConnexion.Close();
}
}
}
我有登录信息的页面:
public partial class Main : Form
{
public Login login;
public Main()
{
InitializeComponent();
button2.Enabled = ViewModel.Instance.IsAdmin;
if(button2.Enabled == true)
{
label3.Text = "admin";
}
else
{
label3.Text = "operator";
}
}
public Main(Login login)
{
InitializeComponent();
label3.Text = login.textBox1.Text;
this.login = login;
if (login.textBox1.Text == "admin" && login.textBox2.Text == "root")
{
button2.Enabled = true;
}
else
{
button2.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
Repair form3 = new Repair();
this.Hide();
form3.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Admin form4 = new Admin();
this.Hide();
form4.Show();
}
private void button3_Click(object sender, EventArgs e)
{
new Login().Show();
this.Hide();
//MessageBox.Show("Vous êtes déconnecté");
}
}
我尝试了“if”条件,但它似乎不起作用,或者至少如果我在运算符中连接它不会进入“else”状态。
谢谢!
答案 0 :(得分:0)
好我可以建议你一个更简单的方法吗? 。因为它似乎是一个Winform应用程序 在program.cs中添加一个静态变量,并添加为UserType
Public Static String UserType="";
然后在您正在执行
的页面中 if (login.textBox1.Text == "admin" && login.textBox2.Text == "root")
{
button2.Enabled = true;
Program.UserType="admin";
}
else
{
button2.Enabled = false;
Program.UserType="User";
}
以您的DataRetreiwal形式
public Repair()
{
InitializeComponent();
SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
maConnexion.Open();
SqlCommand command = maConnexion.CreateCommand();
if (Program.UserType == "admin")
{
command.CommandText = "SELECT * FROM FailOnly.";
}
else
{
command.CommandText = "SELECT * FROM FailOnly WHERE ReportingOperator != NULL";
}
SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
maConnexion.Close();
}