[英]Milliseconds not showing in SQL output for DateTime type
I am having a problem seeing the millisecond component of a DATETIME
type column.我在查看DATETIME
类型列的毫秒分量时遇到问题。
The code that defines the table is:定义表的代码是:
string command = string.Format("CREATE TABLE {0}(EventDateTime DATETIME, layer3Id VARCHAR(35), EventType VARCHAR(55), Protocol VARCHAR(30), MessageBody VARCHAR(MAX), MessageContent VARCHAR(MAX))", tableName);
SendSqlCommand(command);
The code that populates the row is :填充行的代码是:
string insertCommand = string.Format(@"INSERT INTO {0} VALUES (@p1, @p2, @p3, @p4, @p5, @p6)", tablename);
_con.Open();
using (SqlCommand cmd = new SqlCommand(insertCommand, _con))
{
cmd.Parameters.AddWithValue("@p1", record.EventDateTime);
cmd.Parameters.AddWithValue("@p2", record.layer3Id);
cmd.Parameters.AddWithValue("@p3", record.EventType);
cmd.Parameters.AddWithValue("@p4", record.Protocol);
cmd.Parameters.AddWithValue("@p5", record.MessageBody);
cmd.Parameters.AddWithValue("@p6", record.MessageContent);
cmd.ExecuteNonQuery();
}
where the EventDateTime
string value is something like 2016/01/09 16:10:08.500
其中EventDateTime
字符串值类似于2016/01/09 16:10:08.500
When I run a query on the table I get back this: 09/01/2016 16:10:08
当我在表上运行查询时,我返回: 09/01/2016 16:10:08
No milliseconds.没有毫秒。 I don't know if the values are going to the database or if it is the wrong type to use or if the query is not responding correctly:我不知道这些值是否会进入数据库,或者是否使用了错误的类型,或者查询是否没有正确响应:
TestTable.Take (100)
So lets assume you have those values in your database:因此,让我们假设您的数据库中有这些值:
By running pure sql statement you should be able to retrieve datetime in the format you would anticipate, with milliseconds通过运行纯 sql 语句,您应该能够以您预期的格式检索日期时间,以毫秒为单位
And in your C#, as an example, you can control the format, here is an example:在你的 C# 中,作为一个例子,你可以控制格式,这是一个例子:
var res = db.MyTables.Take(100);
foreach(var date in res)
{
Console.WriteLine(Convert.ToDateTime(date.EventDateTime).ToString("dd/MM/yyyy hh:mm:ss.fff"));
}
Here is the output:这是输出:
There are milliseconds.有毫秒。 You just don't display them.你只是不显示它们。
So, when you read the time from the database, you need to format it for output, like this:因此,当您从数据库中读取时间时,您需要对其进行格式化以进行输出,如下所示:
string s;
s.Format("{0:yyy-MM-dd hh:mm:ss.fff}", record.EventDateTime)
Now you can display the variable s
现在您可以显示变量s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.