如何从“SelectWhere”语句中读取返回值,每次运行时标签中都没有返回值,也没有语法错误。
command.CommandText = "select product_price from product where product_name='"+x+"';";
connection.Open();
Reader = command.ExecuteReader();
while(Reader.Read()){
Price_label.Content = "" + Reader.GetString(0);
}
connection.Close();
If the product_price
列不是类型TEXT
在 MySQL 中,Reader.GetString(0)
将(取决于 Oracle 如何实现读取器)抛出异常或返回空字符串。我认为后者正在发生。
通过a检索值DataReader
要求您知道数据类型。您不能简单地读取每种类型字段的字符串。例如,如果数据库中的字段是Integer,则需要使用GetInt32(...)
。如果它是一个DateTime
use GetDateTime(...)
. Using GetString
on a DateTime
字段将不起作用。
EDIT
这就是我编写此查询的方式:
using (MySqlConnection connection = new MySqlConnection(...))
{
connection.Open();
using (MySqlCommand cmd = new MySqlCommand("select product_price from product where product_name='@pname';", connection))
{
cmd.Parameters.AddWithValue("@pname", x);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
StringBuilder sb = new StringBuilder();
while (reader.Read())
sb.Append(reader.GetInt32(0).ToString());
Price_label.Content = sb.ToString();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)