我收到的日期是2014年12月33日上午12:00:00,当时MySQL数据库中的日期是2014-03-22。谁能告诉我为什么它会回归一个奇怪的日子?我假设它与Convert Zero Datetime
有关..但我不完全确定。
代码:
public MySqlDataReader readerDB(string strSelectStmnt)
{
//initializing command
var comm = new MySqlCommand(strSelectStmnt, conn);
//datareader for reading values from table
dr = comm.ExecuteReader();
//return the information from that was gathered by the datareader
return dr;
}
while (dr.Read())
{
Console.WriteLine(Convert.ToString(dr.GetValue(0)));
Console.WriteLine(Convert.ToString(dr.GetValue(1)));
Console.WriteLine(Convert.ToString(dr.GetValue(2)));
}
我的连接字符串:
string MyConString = "Server=" + strIP + ";" +
"PORT=" + strPort + ";" +
"DATABASE=" + strDB + ";" +
"Persist Security Info=no;" +
"Convert Zero Datetime=True;" +
"username=" + strDBUID + ";" +
"password=" + strDBPWD + ";";
我的查询:
var dr = db.readerDB("SELECT `trial_1_end` , `trial_2_end` , `permanent_end` FROM " +
DataTable + " WHERE item_name= '"+ strTmpSubscription +
"' AND username='" + User + "'AND password='" + Password + "'");
我的结果(来自Console.WriteLine
):
31/1/0001 12:00:00 AM 31/1/0001 12:00:00 AM 33/22/2014 12:00:00 AM
解答:
DateTime.ParseExact(dr.GetDateTime(0).ToString("dd/MM/yyyy"), "dd/MM/yyyy", null)
答案 0 :(得分:2)
You can use the SqlDataReader
method GetDateTime
:
Console.WriteLine(dr.GetDateTime(0).ToString());
And you can as well give it your custom formatting like:
Console.WriteLine(dr.GetDateTime(0).ToString("dd/MM/yy H:mm:ss"));