C# MySql“选择位置”和C

C# MySql“选择位置”和C,c#,mysql,wpf,C#,Mysql,Wpf,如何从Select Where语句中读取返回值,每次运行时标签中没有返回值,也没有语法错误 command.CommandText = "select product_price from product where product_name='"+x+"';"; connection.Open(); Reader = command.ExecuteReader(); while(Reader.Read()){

如何从Select Where语句中读取返回值,每次运行时标签中没有返回值,也没有语法错误

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();

你必须为你的阅读器创建一个变量

command.CommandText = "select product_price from product where product_name='"+x+"';";
try {
connection.Open();
SqlReader reader = command.ExecuteReader();
while(reader.Read()){


    Price_label.Content = "" + Reader.GetString(0);

}
} catch (Exception) {}
finally {
connection.Close();
}
如果product_price列在MySQL中不是TEXT类型,则Reader.GetString0将根据Oracle如何实现读取器而抛出异常或返回空字符串。我认为后者正在发生

通过DataReader检索值需要知道数据类型。不能简单地为每种类型的字段读取字符串。例如,如果数据库中的字段是整数,则需要使用GetInt32。。。。如果是DateTime,请使用GetDateTime。。。。在DateTime字段上使用GetString将不起作用

编辑 以下是我编写此查询的方式:

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();
        }
    }
}

在我的评论之后,你的方法有三个问题,它们不是你问题的一部分:

SQL注入。 正在泄漏的资源。 坏习惯,+用于铸造的线是…嗯…不好,也没有必要。 因此,更正确的代码版本如下所示:

// using utilizes the IDisposable-Interface, whcih exists to limit the lifetime
// of certain objects, especially those which use native resources which
// otherwise might be floating around.
using(YourConnectionType connection = new YourConnectionType("connectionstring"))
{
    connection.Open(); // You might want to have this in a try{}catch()-block.

    using(YourCommandType command = connection.CreateCommand())
    {
        command.CommandText = "select product_price from product where product_name=@NAME;";
        command.Parameters.Add("NAME", YourTypes.VarChar);
        command.Parameters[0].Value = x; // For your own sanity sake, rename that variable!

        using(YourReaderType reader = command.ExecuteReader())
        {
            while(reader.Read()) // If you're expecting only one line, change this to if(reader.Read()).
            {
                Price_label.Content = reader.GetString(0);
            }
        }
    }
} // No need to close the conenction explicit, at this point connection.Dispose()
  // will be called, which is the same as connection.Close().

你应该写@pname而不写,否则它将无法工作

而不是:

从产品中选择产品价格,其中产品名称=“@pname”

你应该这样写:

// using utilizes the IDisposable-Interface, whcih exists to limit the lifetime
// of certain objects, especially those which use native resources which
// otherwise might be floating around.
using(YourConnectionType connection = new YourConnectionType("connectionstring"))
{
    connection.Open(); // You might want to have this in a try{}catch()-block.

    using(YourCommandType command = connection.CreateCommand())
    {
        command.CommandText = "select product_price from product where product_name=@NAME;";
        command.Parameters.Add("NAME", YourTypes.VarChar);
        command.Parameters[0].Value = x; // For your own sanity sake, rename that variable!

        using(YourReaderType reader = command.ExecuteReader())
        {
            while(reader.Read()) // If you're expecting only one line, change this to if(reader.Read()).
            {
                Price_label.Content = reader.GetString(0);
            }
        }
    }
} // No need to close the conenction explicit, at this point connection.Dispose()
  // will be called, which is the same as connection.Close().
从产品中选择产品价格,其中产品名称=@pname


数据库中真的有相关记录吗?也许你也可以把名字当作一个参数来传递。我做了,但标签上仍然没有返回,所以我认为读卡器部分有问题。你是VB6背景的,是吗?首先,C是一种静态语言,所以要摆脱打字的习惯。第二,你想看看。此外,您应该通过使用来使用IDisTable接口。从您的代码中,我看不出该命令是否连接到该连接。连接正确吗?哈哈,好的,我会去掉这个:D,谢谢BobbyNope的帮助,你的代码不会工作,因为你没有使用reader,而是使用reader来实际读取值。是的。谢谢当然,您必须编写reader.GetString0;而不是Reader.GetString0;我将它从GetString更改为GetInt32,仍然没有返回值,列类型为Int。您是否确保确实存在与x匹配的记录?是的,我在Mysql命令上对其进行了测试,结果有效,即使在用已知产品名称替换x的代码中,仍然没有返回值。只是注意到代码中有一个错误-创建命令时,第二个参数必须是connection,而不是conn,因为conn不存在:-