从 MS Access 检索数据并以另一种形式显示到标签
Posted
技术标签:
【中文标题】从 MS Access 检索数据并以另一种形式显示到标签【英文标题】:Retrieving data from MS Access and display it to label in another form 【发布时间】:2015-02-23 23:50:14 【问题描述】:我在Form1
中的btnLogin
中有这段代码,用于从MS Access database
获取数据,我希望它显示在Form2
using 标签中,但我不知道如何将其传递给Form2
private void btnLogin_Click(object sender, EventArgs e)
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
if ((this.txtUser.Text == "admin") && (this.txtPass.Text == "admin"))
Form1 main = new Form1();
main.txtHere.Text = txtUser.Text;
main.Show();
this.Hide();
else
command.CommandText = "select * from StudentsTBL where LastName='" + txtUser.Text + "' and FirstName='" + txtPass.Text + "'";
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
while (reader.Read())
count = count + 1;
//count++;
if (count == 1)
MessageBox.Show("Login Successful!");
List<String> stdNo = new List<String>();
List<String> middleName = new List<String>();
List<String> section = new List<String>();
List<String> year = new List<String>();
List<String> sem = new List<String>();
List<String> address = new List<String>();
List<String> dob = new List<String>();
List<String> gender = new List<String>();
List<String> age = new List<String>();
List<String> contact = new List<String>();
List<String> desc = new List<String>();
command = new OleDbCommand("select * from StudentsTBL", connection);
reader = command.ExecuteReader();
while (reader.Read())
stdNo.Add(reader["StudentNo"].ToString());
middleName.Add(reader["Middle"].ToString());
section.Add(reader["stdSection"].ToString());
year.Add(reader["stdYear"].ToString());
sem.Add(reader["stdSem"].ToString());
address.Add(reader["stdAddress"].ToString());
dob.Add(reader["stdDob"].ToString());
gender.Add(reader["stdGender"].ToString());
age.Add(reader["stdAge"].ToString());
contact.Add(reader["ContactNo"].ToString());
desc.Add(reader["stdDesc"].ToString());
StudentProfile stdProfile = new StudentProfile(txtUser.Text, txtPass.Text);
stdProfile.Show();
this.Hide();
else if (count > 1)
MessageBox.Show("Duplicate username and password!!");
else
MessageBox.Show("Login Failed!");
connection.Close();
来源:Getting data from MS Access database and display it in a listbox
【问题讨论】:
嗯,什么Form2
?你的 Label
字段的名称是什么?
我要将数据显示到form2
Forms are just classes
如果您试图让 Form1 与 Form2 对话,您可以使用委托或构造函数注入。参考***.com/a/27179975/1862333
【参考方案1】:
为什么你不使用结构? 它会让你的喜欢很容易你可以一次处理很多信息 变量。
你为什么不做一个全局变量? 它可以从您应用程序的任何部分获得。
我在GitHub 有这样的项目,你可以下载作为示例参考
structure UserInfo
public string stdNo;
public string middleName;
public string section;
public string year;
public string sem;
public string address;
public string dob;
public string gender;
public string age;
public string contact;
public string desc;
//===>Define a global variable to handle
public static UserInfo LoggedUsrInfo = new UserInfo();
private void btnLogin_Click(object sender, EventArgs e)
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
if ((this.txtUser.Text == "admin") && (this.txtPass.Text == "admin"))
Form1 main = new Form1();
main.txtHere.Text = txtUser.Text;
main.Show();
this.Hide();
else
command.CommandText = "select * from StudentsTBL where LastName='" + txtUser.Text + "' and FirstName='" + txtPass.Text + "'";
DataTable Tbl = new DataTable();
OleDbDataReader reader = command.ExecuteReader();
Tbl.Load(reader,LoadOption.OverwriteChanges);//===>Retrieve data and load to datatble
reader.Close();//Close the reader
if (Tbl.Rows.Count > 0) //Count if the data table retrieve some info
DataTable TblInfo = new DataTable();
MessageBox.Show("Login Successful!");
command = new OleDbCommand("select * from StudentsTBL", connection);
reader = command.ExecuteReader();
TblInfo.Load(reader,LoadOption.OverwriteChanges);
reader.Close();//Close the reader
LoggedUsrInfo.stdNo = TblInfo.Rows[0]["StudentNo"].ToString();
LoggedUsrInfo.middleName = TblInfo.Rows[0]["Middle"].ToString();
LoggedUsrInfo.section = TblInfo.Rows[0]["stdSection"].ToString();
LoggedUsrInfo.year = TblInfo.Rows[0]["stdYear"].ToString();
LoggedUsrInfo.sem =TblInfo.Rows[0]["stdSem"].ToString();
LoggedUsrInfo.address = TblInfo.Rows[0]["stdAddress"].ToString();
LoggedUsrInfo.dob = TblInfo.Rows[0]["stdDob"].ToString();
LoggedUsrInfo.gender = TblInfo.Rows[0]["stdGender"].ToString();
LoggedUsrInfo.age = TblInfo.Rows[0]["stdAge"].ToString();
LoggedUsrInfo.contact = TblInfo.Rows[0]["ContactNo"].ToString();
LoggedUsrInfo.desc = TblInfo.Rows[0]["stdDesc"].ToString();
StudentProfile stdProfile = new StudentProfile(txtUser.Text, txtPass.Text);
stdProfile.Show();
this.Hide();
else if (count > 1)
MessageBox.Show("Duplicate username and password!!");
else
MessageBox.Show("Login Failed!");
connection.Close();
/*
you can access to UserInfo calling in this way from your Form2
Load Event
Label1.Text = Form1.LoggedUsrInfo.middleName;
*/
【讨论】:
以上是关于从 MS Access 检索数据并以另一种形式显示到标签的主要内容,如果未能解决你的问题,请参考以下文章