Mysql DB returns 错误的日期格式 (C#)

Mysql DB returns wrong date format (C#)

这是一个非常简单的问题,关于我的 C#-App 的混淆情况:

使用:
C# 桌面应用程序/Mysql 5.5

发生了什么:
我的应用程序是 selection 数据库中的一些数据 (Select date from mytable)。 date 列的格式为 yyyy-mm-dd。在我的 C#-App 中检索数据后,我得到了文化敏感的输出。在我的例子中,格式为 dd.mm.yyyy。查看我的调试截图。

注意:变量listDate来自类型string!

我的输出类似于 DateTime 类型。但我从来没有把它转换成日期时间。列 (db) date 设置为格式 date.

为什么我会得到这种格式?是数据库问题还是应用程序问题? 我的目标是获得与数据库内部相同的格式 yyyy-mm-dd

感谢您的帮助。非常感谢!!!

编辑 1:

我的代码:

public static void DB_Select(string s, params List<string>[] lists)
        {
            try
            {
                using(var conn = new MySqlConnection(ServerConnection))
                {
                    conn.Open();
                    MySqlCommand cmd = conn.CreateCommand();
                    cmd.CommandType = CommandType.Text;
                    string command = s;
                    cmd.CommandText = command;
                    using(var sqlreader = cmd.ExecuteReader())
                    while (sqlreader.Read())
                    {
                        if (sqlreader[0].ToString().Length > 0)
                        {
                            for (int i = 0; i < lists.Count(); i++)
                            {
                                lists[i].Add(sqlreader[i].ToString());
                            }
                        }
                        else
                        {
                            foreach (List<string> save in lists)
                            {
                                save.Add("/");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while selecting data from database!\nDetails: " + ex);
            }
        }

无法想象这可能是问题所在。这一切所做的就是遍历输出并将单列保存到不同的传递列表中 (params List<string>[])

使用:

Services.DB_Select("SELECT date FROM myTable WHERE something = something")

所以一切都保存在字符串中,没有任何转换。它只是关于 select 和输出 db -> c#。我 select 使用日期格式并获取文化敏感的日期时间值。这怎么可能?

编辑 2:(解决方案)

如果我得到它 SELECT CONCAT(date) FROM mytable...(告诉数据库将 (col)date 作为字符串处理,我得到干净的输出 yyyy-mm-dd !

没有 concat,我的 dateTime 输出对文化敏感。所以这一定是我的 date 列的日期格式有问题。但为什么 - 真的不知道。

如果你想要,可以这样获取:

string example = "20.08.2013 00:00:00";
DateTime result = GetDate(example);

通过使用此方法:

public static DateTime GetDate(string stringFormat)
{
    var date = stringFormat.Split(' ')[0].Split('.').Select(x => Int32.Parse(x)).ToArray();
    return new DateTime(date[2], date[1], date[0]);
}

由于您没有提供如何从数据库中获取数据,所以很难说。可能有两种选择 - 1) 在检索数据时进行某种隐式转换,以便从 db 获取格式化字符串或 2) 在获取数据后在某处进行转换。

您可以做的是转换为 DateTime listDate.Select(d => DateTime.ParseExact(d,"dd.MM.yyyy hh:mm:ss",CultureInfo.InvariantCulture)) 然后将其格式化回您需要的格式。

更好的解决方案当然是直接从数据库中获取 DateTime 中的日期。请post你的代码。

编辑

问题在于 sqlreader[i].ToString() - 这会从环境中获取您的文化并进行转换。在那种情况下,我会检查值的类型,如果是 DateTime,则使用 ToString("yyyy-MM-dd") 而不是简单的 ToString()

来自文档:

This method uses formatting information derived from the current culture. In particular, it combines the custom format strings returned by the ShortDatePattern and LongTimePattern properties of the DateTimeFormatInfo object returned by the Thread.CurrentThread.CurrentCulture.DateTimeFormat property. For more information, see CultureInfo.CurrentCulture. Other overloads of the ToString method enable you to specify the culture whose formatting to use and to define the output pattern of the DateTime value.

好吧,我的一个学院告诉我一个解决方案。至少对我来说这听起来合乎逻辑:

col 本身来自 date 类型,它是 dateTime 的简短版本。当使用像 SELECT date FROM mytable 这样的清晰 select 时,输出将作为 DateTime。这就是为什么我也在我的输出中得到时间部分。

进行SELECT CONCAT(date) FROM mytable时,该值像字符串一样处理。我自己找到了一个解决方法。这只会输出日期本身。

奇怪的行为。肯定是因为我使用这个日期格式的列很长时间了。从来没有遇到过任何问题。

非常感谢您的关注!