[英]How can I store the value of a MySqlCommand in C# to a local variable?
I am using MySql in Windows Form Application. 我在Windows窗体应用程序中使用MySql。 I want to store the result of a MySqlCommand in a string and then assign the contents of that string to a label control on my Windows form so that the label text will display the result of the MySqlCommand.
我想将MySqlCommand的结果存储在字符串中,然后将该字符串的内容分配给Windows窗体上的标签控件,以便标签文本将显示MySqlCommand的结果。 I have tried the bottom code but that doesn't work.
我已经尝试了底部的代码,但这是行不通的。
public void WhoLogIn()
{
_con.Open();
MySqlCommand NewCommand = new MySqlCommand("select titleandfullname from users where username ='" + Variables.whologin + "';", _con);
//Variables.whologin is a public const string in a class declared in another file.
MySqlDataReader result;
result = NewCommand.ExecuteReader();
string _nameofwhologin;
while (result.Read())
{
_nameofwhologin = result.GetString(0);
}
_nameofwhologin = label2.Text;
}
I would like help on how to achieve this using corrections to the code or the proposal of a different method altogether. 我希望获得有关如何通过更正代码或完全采用其他方法的建议来实现此目的的帮助。
Change your code to this; 将您的代码更改为此;
public void WhoLogIn()
{
_con.Open();
MySqlCommand NewCommand = new MySqlCommand("select titleandfullname from users where username ='" + Variables.whologin + "';", _con);
//Variables.whologin is a public const string in a class declared in another file.
MySqlDataReader result;
result = NewCommand.ExecuteReader();
string _nameofwhologin;
while (result.Read())
{
_nameofwhologin = result.GetString(0);
}
label2.Text=_nameofwhologin;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.