How to read from mysql datetime field

后端 未结 2 1168
长发绾君心
长发绾君心 2021-01-28 03:18

I\'m trying to read from mysql field with datetime type and I was trying everything and always it\'s return field name only.

Here is my code:

using (MySq         


        
相关标签:
2条回答
  • 2021-01-28 03:54

    The issue is your SELECT statement, you have single quotes ' instead of back ticks around sDate and so the value of that field is in fact "sDate".

    Or as Jon Skeet stated, get rid of the back ticks entirely because they exist for keywords only, must like the [] do for MSSQL.

    0 讨论(0)
  • 2021-01-28 04:01

    Use

    dataReader.GetDateTime(dataReader.GetOrdinal("sDate"))
    

    instead of

    dataReader["sDate"] + ""
    

    and remove the ticks around your columns in the sql, e.g.

    sDate instead of 'sDate'. You need them only for reserved words or if you have spaces in your column names:

    9.3. Reserved Words

    2.2. Reserved Words in MySQL 5.1

    0 讨论(0)
提交回复
热议问题