2014-01-21 38 views
0

我不確定我的問題有答案! 我有一個日期列的MYSQL表。我希望把它在C#中的字符串不MYSQL日期時間 但事情是我不知道列Name.I知道該怎麼做,如果知道列名MYSQL將日期列作爲字符串

SELECT DATE_FORMAT(X, '%Y/%m/%d') AS X FROM Y 

,但我不知道該怎麼做時,我不知道專欄名稱?

回答

0

什麼MySQL連接您的使用,但假設它的ADO兼容以下應該工作,我不知道:

string dateString; 
using (IDbConnection connection = new MySqlConnection()) { 
    connection.ConnectionString = "YOUR STRING"; 
    connection.Open(); 
    try { 
     using (IDbCommand command = connection.CreateCommand()) 
     { 
      command.CommandText = "SELECT DATE_FORMAT(X, '%Y/%m/%d') AS X FROM Y"; 
      using (IDataReader reader = command.ExecuteReader()) { 
       dateString = reader.GetString(0); 

       // Alternately you can read the data as a DateTime and use 
       // .NET's formatting. 
       //dateString = reader.GetDateTime(0).ToShortDateString(); 
      } 
     } 
    } 
    finally { 
     connection.Close(); 
    } 
} 

編輯:

迴應澄清你的評論,看到這個question。您需要查詢information_schema表並過濾表名和數據類型。

+0

感謝您的回放,但我的問題是:「如果我不知道列名稱即X的查詢是什麼」 – user2326448