这应该是一个简单的解决方案,但是Visual Studio 2012给了我一些错误,比如sqlCon是一个字段,但是它被用作类型,而Textbox1也有同样的错误。。。可能缺少程序集引用或正确的连接导入?我想继续这条简单的路线。
MySqlConnection sqlCon = new MySqlConnection("Server=***;Port=***;Database=***;Uid=***;Pwd=***;");
MySqlCommand commandText = new MySqlCommand ("SELECT count(Dues) From Students");
sqlCon.CommandText = "SELECT * count(Dues) FROM Students";
sqlCon.Connection = sqlCon;
TextBox1.Text = sqlCon.ExecuteScalar().ToString();
最佳答案
打开连接
使用using
语句
使用Try-catch
块
片段,
string connStr = "Server=***;Port=***;Database=***;Uid=***;Pwd=***;";
string query = "SELECT count(Dues) From Students";
using(MySqlConnection sqlCon = new MySqlConnection(connStr))
{
using(MySqlCommand sqlComm = new MySqlCommand())
{
sqlComm.Connection = sqlCon;
sqlComm.CommandText = query;
try
{
sqlCon.Open();
TextBox1.Text = sqlComm.ExecuteScalar().ToString();
}
catch(MySqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
关于c# - 查询文本框时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14050422/