我希望我的c#程序从mysql数据库中获取数据,并将该数据保存到变量类型double中。我正在尝试,但仍然无法做到。
`
MySqlDataReader read = null;
string selectCmd = "SELECT Saldo from cuentas where NoCuenta=" + cuenta + ";";
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
MySqlCommand MyCommand2 = new MySqlCommand(selectCmd, MyConn2);
MyConn2.Open();
read = MyCommand2.ExecuteReader();
MessageBox.Show("read: " +read);
bool saldoI = read.Read();
double saldo = Convert.ToDouble(saldoI);
MessageBox.Show("Aqui se muestra el saldo inicial " + read);
double saldoFinal = saldo - monto;
`
我知道当我从布尔类型变量对其进行转换时,它返回1,但是我要求它返回数据类型为double。示例:300.2
为什么不:
if (read.Read())
double saldo = Convert.ToDouble(reader[0]);
的read.Read()
返回值,如果的确从您的查询,假一返回行,如果没有,或者你看他们都返回true。
上面链接中的示例:
private static void CreateCommand(string queryString,
string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(String.Format("{0}", reader[0]));
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句