帮忙看个小问题!
我在mainform 这窗体中有个lable_username(public) 控件用来存储获取的用户名字,我在另一个窗体中通过lable_username的值来查询当前用户的信息;可是查询不出当前用户的信息。各位大侠帮忙看下:
private void tbx_queryUserInformation_Click(object sender, EventArgs e)
{
MainForm mfrm2 = new MainForm();
MySqlConnection con = new MySqlConnection("Server=localhost;User Id=root;Password=wencui2006;Persist Security Info=True;Database=ken;charset=utf8");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sql = "select ID,Users as 姓名,PW as 密码 from account where users='“+mrfm2.label_username.text+”'";
MySqlDataAdapter adp = new MySqlDataAdapter(sql,con);
DataSet ds = new DataSet();
adp.Fill(ds,"account");
bindingSource_Look.DataSource=ds.Tables["account"];
dataGridView1.DataSource = bindingSource_Look;
MySqlCommandBuilder cmd = new MySqlCommandBuilder(adp);
con.Close();
}
哪位大虾来指点下,或者有什么更好的方法?
------解决方案--------------------不能new 新窗体当然是空的了 引用 之前的窗体
------解决方案--------------------MainForm mfrm2 = new MainForm();
你这样是新建一个 MainForm 了,你应该寻找已经打开的那个 MainForm 实例,假如 MainForm 是主界面,
string sql = "select ID,Users as 姓名,PW as 密码 from account where users='“+ ((MainForm )Application.OpenForms[0]).label_username.text+”'"
------解决方案--------------------在MainForm里定义一个变量(全局)存储这个用户名
public static string name=label_username.text
然后在另一个窗口调用这个变量
string sql = "select ID,Users as 姓名,PW as 密码 from account where users='“+mrfm2.name+”'";
------解决方案--------------------