我在下面的while循环中收到错误,表示"无法隐式转换类型' float'到'字符串'"。
try
{
string ab = "select Sum(amount) from matriculationfee ";
SqlCommand myCommand = new SqlCommand(ab, con);
SqlDataReader myReader;
con.Open();
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
txtbalance.Text=(((float)myReader["amount"]).ToString());
//It Says here that it Cannot Implicity convert type 'float' to 'string
}
}
catch (SqlException w)
{
MessageBox.Show("Erro!" + w.Message);
}
请帮助我。
答案 0 :(得分:2)
首先,您需要命名要返回的列:
string ab = "select Sum(amount) AS amount from matriculationfee ";
然后我建议您简化转换代码:
var amount = float.Parse(myReader["amount"]);
txtbalance.Text= amount.ToString();
答案 1 :(得分:0)
将结果总和如下:
string ab = "select SUM(amount) AS total from matriculationfee ";
然后像这样参考:
var amount = myReader.GetFloat("total");
txtbalance.Text= amount.ToString();