0

I am not sure my question has answer! I have a MYSQL table with a date column. I want get it in C# as string not MYSQL date time but matter is I don't know column Name.I know how do it if know column name

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

but i don't know how do it when i don't know column Name?

user2326448
  • 85
  • 1
  • 11

1 Answers1

0

I don't know what MySql connector your using but assuming it's ADO compatible the following should work:

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

EDIT:

In response to clarification from your comment, see this question. You'll need to query the information_schema table and filter on the table name and data type.

Community
  • 1
  • 1
Lee Hiles
  • 1,125
  • 7
  • 9