简体   繁体   English

如何使用 Devart dotConnect Express MySQL 获得 MySQL 时间分数?

[英]How to get MySQL time fraction with Devart dotConnect Express MySQL?

Assume that I have a table like this:假设我有一个这样的表:

CREATE TABLE `table1` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `date` date,
  `timestamp2` timestamp(2),
  `datetime3` datetime(3),
  `time6` time(6),
  PRIMARY KEY (`id`)
);

Here's the sample inserts:这是样本插入:

insert into table1(`date`,`timestamp2`,datetime3,time6)
values('2000-01-01','2001-01-01 01:01:01.23',
       '2002-01-01 01:01:01.243','01:01.234893');

insert into table1(`date`,`timestamp2`,datetime3,time6) 
values(null,null,null,null);

and I get the data in C# like this:我得到 C# 中的数据,如下所示:

using Devart.Data.MySql;

DataTable dt = new DataTable();
StringBuilder sb = new StringBuilder();

using (MySqlConnection conn = new MySqlConnection(ConnString))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = "select * from table1;";
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        da.Fill(dt);
        conn.Close();
    }
}

int row = 0;

foreach (DataRow dr in dt.Rows)
{
    row++;

    sb.AppendLine();
    sb.AppendLine("Row " + row);

    foreach (DataColumn dc in dt.Columns)
    {
        object ob = dr[dc.ColumnName];
        string typeName = ob.GetType().ToString();
        sb.AppendLine($"{dc.ColumnName} = {typeName} | value = " + ob + "");
    }
}

richTextBox1.Text = sb.ToString();

and here's the output:这是 output:

Row 1
id = System.Int64 | value = 1
date = System.DateTime | value = 01/01/2000 12:00:00 AM
timestamp2 = System.DateTime | value = 01/01/2001 1:01:01 AM
datetime3 = System.DateTime | value = 01/01/2002 1:01:01 AM
time6 = System.TimeSpan | value = 01:01:00.2348930

Row 2
id = System.Int64 | value = 2
date = System.DBNull | value = 
timestamp2 = System.DBNull | value = 
datetime3 = System.DBNull | value = 
time6 = System.DBNull | value = 

As you can see that the Devart Express dotConnect MySQL returns time fraction in DateTime in C#. The time fraction is lost.如您所见,Devart Express dotConnect MySQL 在 C# 中返回DateTime中的时间分数。时间分数丢失了。

How to get the time fraction?如何获得时间分数?

*Note: For some reason I must use Devart Express dotConnect. *注意:出于某种原因,我必须使用 Devart Express dotConnect。

Fractional second values are stored in DataTime structure.小数秒值存储在 DataTime 结构中。

Kindly note, that the default ToString method of DateTime doesn't show the fraction of a second.请注意,DateTime 的默认 ToString 方法不显示秒的小数部分。

To extract the string representation of a time's millisecond component, call the date and time value's DateTime.ToString(String) or ToString method, and pass the fff or FFF custom format pattern alone or with other custom format specifiers as the format parameter: https://learn.microsoft.com/en-us/do.net/standard/base-types/how-to-display-milliseconds-in-date-and-time-values .要提取时间的毫秒分量的字符串表示形式,请调用日期和时间值的DateTime.ToString(String)ToString方法,并单独传递 fff 或 FFF 自定义格式模式或与其他自定义格式说明符一起作为格式参数传递:https: //learn.microsoft.com/en-us/do.net/standard/base-types/how-to-display-milliseconds-in-date-and-time-values

To see the time fraction in your test application:要查看测试应用程序中的时间分数:

foreach (DataColumn dc in dt.Columns)
{
    var ob = dr[dc.ColumnName];
    string typeName = ob.GetType().ToString();

    string val;
    if (typeName == "System.DateTime") val = ((DateTime)ob).ToString("yyyy.MM.dd hh: mm:ss.fff");
    else val = ob.ToString();

    Console.WriteLine($"{dc.ColumnName} = {typeName} | value = " + val + "");
}

The result:结果:

Row 1
id = System.Int64 | value = 1
date = System.DateTime | value = 2000.01.01 12: 00:00.000
timestamp2 = System.DateTime | value = 2001.01.01 01: 01:01.230
datetime3 = System.DateTime | value = 2002.01.01 01: 01:01.243
time6 = System.TimeSpan | value = 01:01:00.2348930
 
Row 2
id = System.Int64 | value = 2
date = System.DBNull | value =
timestamp2 = System.DBNull | value =
datetime3 = System.DBNull | value =
time6 = System.DBNull | value =
Press any key to continue . . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM