我完全没有编程,我正在学习,类,方法与mysql的连接。
所以我有一个名为take
的类,我有一个从数据库表中获取最后一个值的方法。
namespace Budget
{
class take
{
private MySqlConnection connection;
private string datasource;
private string port;
private string username;
private string password;
public take()
{
Initialize();
}
private void Initialize()
{
datasource = "localhost";
port = "3306";
username = "root";
password = "root";
string connectionString;
connectionString = "DATASOURCE=" + datasource + ";" + "PORT=" + port + ";" + "USERNAME=" + username + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
private bool OpenConnection()
{
connection.Open();
return true;
}
private bool CloseConnection()
{
connection.Close();
return true;
}
public decimal budget()
{
string query =
@"SELECT balance
FROM history
ORDER BY id DESC
LIMIT 1 ";
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
return Convert.ToDecimal(reader.GetValue(0));
else
return 0m;
}
}
else
return 0m;
}
}
}
我想把这个值放在我的主Form FormBox中。但是当我在主要主要表格中写作时
take.balance();
警告警告1永远不会将字段
Budget.Form1.take
分配给,并且始终具有默认值null
所以问题是,我如何将数据库中的值放到我的主要Form文本框中?