2015-08-13 95 views
0

我與我的數據庫建立了連接,並且我執行了一個查詢以獲取我的表的特定字段的結果,但讀者不在此時輸入,爲什麼?閱讀器無法顯示結果

public void connect() 
    { 
     string connStr = @"Server=xx98.66;Port=xx6;Database=sxxt;Uid=axx;Pwd=admxxx3z;"; 
     MySqlConnection conn = new MySqlConnection(connStr); 

     try 
     { 
      Console.WriteLine("Connecting to MySQL..."); 
      conn.Open(); 

      string stm = "SELECT lastUpdated from soccerseason WHERE caption = 'Eredivisie 2015/1'"; 
      MySqlCommand cmd = new MySqlCommand(stm, conn); 

      MySqlDataReader rdr = cmd.ExecuteReader(); 

      while (rdr.Read()) 
      { 
       Console.WriteLine("stdrstrr" + rdr.GetString(0)); 
      } 

      Console.WriteLine("Connection successfull !"); 

      conn.Close(); 
      Console.ReadLine(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Eccezione => " + ex.ToString()); 
     } 
     Console.WriteLine("Done."); 
    } 
} 
+0

你有沒有直接試一下查詢針對數據庫和檢查結果如何?這是第一步。如果沒有數據,你已經解決了這個問題。讀者沒有閱讀Read()有一些原因。 –

+0

db中有數據 –

+0

該代碼似乎是正確的。所以,可能沒有與你的WHERE匹配的數據,或者你正在連接到不同的數據庫 – Steve

回答

0

這將幫助你,我覺得

略包含此

using MySql.Data.Common; 
using MySql.Data.Types; 
using MySql.Data.MySqlClient; 

更改代碼,並檢查

{ 
     string connStr = @"Server=xx98.66;Port=xx6;Database=sxxt;Uid=axx;Pwd=admxxx3z;"; 
       MySqlConnection conn = new MySqlConnection(connStr); 
     try 
    { 
     string stm = "SELECT lastUpdated from soccerseason WHERE caption = 'Eredivisie 2015/1'"; 

        MySqlCommand cmd= conn.CreateCommand(); 
        cmd.CommandText = @"SELECT lastUpdated from soccerseason WHERE caption = 'Eredivisie 2015/1'"; 
        MySqlDataReader rdr; 
        conn.Open(); 
        rdr= cmd.ExecuteReader(); 
        DataTable dtsoccer= new DataTable(); 
        dtsoccer.Load(rdr); 
        foreach (DataRow row in dtsoccer.Rows) 
        { 
         Console.WriteLine(row["lastUpdated "].ToString()); 
        } 
        conn.Close(); 
        Console.ReadKey(); 

     } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Eccezione => " + ex.ToString()); 
      } 
      Console.WriteLine("Done."); 
     } 
    } 
+0

使用相同的確切查詢,爲什麼讀者現在應該返回一些行? – Steve