如何存储查询结果以获取位置,以便我可以在另一个私有void方法中使用它?我试图用作会话,但我无法让它工作。 C#总是强调它的某些部分是红色的。任何想法都会有所帮助。以下是我如何从数据库中提取信息。
private void LoginButton_Click(object sender, EventArgs e)
{
string commandText = "SELECT RIGHTS FROM [USER] WHERE Username=@p1 and [Password]=@p2";
using (OleDbCommand command = new OleDbCommand(commandText, con))
{
command.Parameters.AddWithValue("@p1", textBox1.Text);
command.Parameters.AddWithValue("@p2", textBox2.Text);
string query = (string)command.ExecuteScalar();
{
if (query == "Manager")
{
string locationText = "SELECT LOCATION FROM [USER] WHERE Username=@p1 and [Password]=@p2";
using (OleDbCommand location = new OleDbCommand(locationText, con))
{
location.Parameters.AddWithValue("@p1", textBox1.Text);
location.Parameters.AddWithValue("@p2", textBox2.Text);
string locationResult = (string)command.ExecuteScalar();
{
}
}
}
}
然后我想在相同的Windows窗体中使用它,以便它只显示用户所在的位置。
private void groupBox5_Enter(object sender, EventArgs e)
{
//Load Employee Name
using (OleDbConnection con = new OleDbConnection(constring))
{
try
{
string query = "SELECT TellerNum FROM EMPLOYEE WHERE Location = locationResult ORDER BY TellerNum ASC";
OleDbDataAdapter da = new OleDbDataAdapter(query, con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "Name");
comboBox14.DisplayMember = "TellerNum";
comboBox14.DataSource = ds.Tables["Name"];
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
我不确定如何调用第一个查询的结果并将其放在第二个查询中。我把" locationResult"在查询中所以我记得那是我需要它去的地方。如果您有任何想法,我很乐意听到。
答案 0 :(得分:1)
您可以将第一个查询的结果存储在类字段中。只需创建:
private string LocationFromQuery;
您班级中的字段,并为其指定值。
答案 1 :(得分:1)
在页面级别的string locationResult
函数之外声明LoginButton_Click
。
说private string locationResult
然后设置此变量值
private void LoginButton_Click(object sender, EventArgs e)
{
//.... all your code
locationResult = (string)command.ExecuteScalar();
}
然后在你的第二个函数下,在sql查询中使用该变量值
private void groupBox5_Enter(object sender, EventArgs e)
{
//.... all your code
string query = "SELECT TellerNum FROM EMPLOYEE WHERE Location ='" + locationResult + "' ORDER BY TellerNum ASC";
//...all your code
}
答案 2 :(得分:1)
也许你应该使用全局变量。
private string Location="";
private void LoginButton_Click(object sender, EventArgs e)
{
string commandText = "SELECT RIGHTS FROM [USER] WHERE Username=@p1 and [Password]=@p2";
using (OleDbCommand command = new OleDbCommand(commandText, con))
{
command.Parameters.AddWithValue("@p1", textBox1.Text);
command.Parameters.AddWithValue("@p2", textBox2.Text);
string query = (string)command.ExecuteScalar();
{
if (query == "Manager")
{
string locationText = "SELECT LOCATION FROM [USER] WHERE Username=@p1 and [Password]=@p2";
using (OleDbCommand location = new OleDbCommand(locationText, con))
{
location.Parameters.AddWithValue("@p1", textBox1.Text);
location.Parameters.AddWithValue("@p2", textBox2.Text);
string Location = (string)command.ExecuteScalar();
{
}
}
}
}
private void groupBox5_Enter(object sender, EventArgs e)
{
if(!Location.Equals("")){
//Load Employee Name
using (OleDbConnection con = new OleDbConnection(constring))
{
try
{
string query = "SELECT TellerNum FROM EMPLOYEE WHERE Location = @p1 ORDER BY TellerNum ASC";
location.Parameters.AddWithValue("@p1", Location);
OleDbDataAdapter da = new OleDbDataAdapter(query, con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "Name");
comboBox14.DisplayMember = "TellerNum";
comboBox14.DataSource = ds.Tables["Name"];
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
我没有尝试过这段代码,但是你需要做的就是让它工作。
但我认为将数据库处理代码放到gui上并不算太幸运。 你应该阅读MVC和MVP。