【问题标题】:MySql "Select Where" and C#MySql“选择位置”和C#
【发布时间】:2012-04-04 22:30:03
【问题描述】:

如何从“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();

【问题讨论】:

  • 数据库中真的有相关记录吗?也许你也可以考虑将名称作为参数传递
  • 我做了,标签上仍然没有返回,所以我认为阅读器部分有问题。
  • 你有VB6背景,是吗?首先,C# 是一种静态语言,因此请摆脱 "" + 类型转换的习惯。其次,你想看at parameterized queries。此外,您应该通过using() 使用IDispoable 接口。
  • 从您的代码中我看不到命令​​是否已连接到连接。连接对了吗?
  • 哈哈好吧,我会摆脱那个:D,感谢鲍比的帮助

标签: c# mysql wpf


【解决方案1】:

如果 product_price 列不是 MySQL 中的 TEXT 类型,Reader.GetString(0) 将(取决于 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();
        }
    }
}

【讨论】:

  • 我把它从GetString()改成了GetInt32(),还是没有返回值,列类型是INT
  • 您确定确实有一条记录与x 匹配吗?
  • 是的,我在 Mysql Command 上对其进行了测试,它工作正常,即使在将 X 替换为已知产品名称的代码中,仍然没有返回值。
  • 刚注意到代码有错误——创建命令时,第二个参数必须是connection,而不是conn,因为conn不存在:-)跨度>
【解决方案2】:

附加到我的评论中,您的方法存在三个问题,这些问题不属于您的问题:

因此,您的代码更正确的版本如下所示:

// 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().

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 不,您的代码不起作用,因为您没有使用reader,而是使用Reader 来实际读取值。
    • 是的。谢谢。当然你必须写 reader.GetString(0);而不是 Reader.GetString(0);
    【解决方案4】:

    @pname 应该不带 '' 否则不起作用。

    代替:

    从 product_name='@pname' 的产品中选择 product_price

    你应该这样写:

    从 product_name=@pname 的产品中选择 product_price

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 2022-07-06
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2010-09-26
      • 2013-02-02
      相关资源
      最近更新 更多