2012-03-20 71 views
3

如何从“Select Where”语句读取返回值,每次运行时没有返回值出现在标签中,也没有语法错误。MySql“Select Where”和C#

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

是否真的有任何数据库中的相关记录?也许你也可以考虑将名称作为参数 – V4Vendetta 2012-03-20 12:53:42

+0

传递给我,但仍然没有返回出现在标签中,所以我认为读者部分有问题。 – 2012-03-20 12:56:40

+2

你来自VB6的背景,是吗?首先,C#是一种静态语言,所以摆脱这种''“+”习惯来进行类型转换。其次,你想看看[参数化查询](http://bobby-tables.com/)。另外,你应该通过'using()'使用'IDispoable'接口。 – Bobby 2012-03-20 12:58:54

回答

3

变量如果product_price列在MySQL TEXT型的没有,Reader.GetString(0)会(取决于如何,读者被甲骨文实现)扔一个异常或返回一个空字符串。我会认为后者正在发生。

通过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(); 
     } 
    } 
} 
+0

我将其从GetString()更改为GetInt32(),仍然没有返回值,并且列类型为INT – 2012-03-20 13:10:00

+0

您确定实际上存在匹配'x'的记录吗? – 2012-03-20 13:11:19

+0

是的,我测试了它的Mysql Command,它的工作原理,即使在代码中用已知的产品名称替换了X,仍然没有返回值。 – 2012-03-20 13:34:43

0

你必须创建你的读者的

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

不,你的代码不会工作,因为你不是使用'reader',而是'Reader'来实际读取值。 – 2012-03-20 12:58:08

+0

是的。谢谢。当然你必须写reader.GetString(0);而不是Reader.GetString(0); – Safari 2012-03-20 12:59:47

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 [email protected];"; 
     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().