简体   繁体   English

从SQLite数据库C#WPF中提取日期时间的问题

[英]Issue with DateTime extract from SQLite Database C# WPF

I have a very freaky mistake... 我有一个非常怪异的错误...

I have two narrow-equals class, to extract data from sqlite Databases 我有两个窄等式类,用于从sqlite数据库中提取数据

I save dates as DATETIME in two SQLite databases. 我在两个SQLite数据库中将日期另存为DATETIME。 My first, contains this method to load datas 我的第一个,包含了这种加载数据的方法

private void LoadData()
        {
            SetConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            string CommandText = "select ID,Name,Solde,Last_Update from Comptes";
            sql_cmd.CommandText = CommandText;
            SQLiteDataReader reader = sql_cmd.ExecuteReader();
            while (reader.Read())
            {
                Compte compte = new Compte();
                compte.ID = reader.GetInt16(reader.GetOrdinal("ID"));
                compte.Nom = reader.GetString(reader.GetOrdinal("Name"));
                compte.Solde = reader.GetDouble(reader.GetOrdinal("Solde"));
                string to = reader.GetString(reader.GetOrdinal("Last_Update"));
                DateTime dt=DateTime.ParseExact(reader.GetString(reader.GetOrdinal("Last_Update")),"yyyy-MM-dd hh:mm:ss",CultureInfo.InvariantCulture);
                compte.Last_Update = dt;
                this.Comptes.Add(compte);

            }
            sql_con.Close();
        }

The second one: 第二个:

private void ExecuteQuery(string txtQuery)
        {
            SetConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            sql_cmd.CommandText = txtQuery;
            sql_cmd.ExecuteNonQuery();
            sql_con.Close();
        }

        private void LoadData()
        {
            SetConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            string CommandText = "SELECT ID,Montant,ID_Emetteur,ID_Recepteur,Description,Date FROM Mouvements";
            sql_cmd.CommandText = CommandText;
            SQLiteDataReader reader = sql_cmd.ExecuteReader();
            while (reader.Read())
            {
                Mouvement mouvement = new Mouvement();
                mouvement.ID = reader.GetInt16(reader.GetOrdinal("ID"));
                mouvement.Montant=reader.GetDouble(reader.GetOrdinal("Montant"));
                mouvement.ID_Emetteur=reader.GetInt32(reader.GetOrdinal("ID_Emetteur"));
                mouvement.ID_Recepteur=reader.GetInt32(reader.GetOrdinal("ID_Recepteur"));
                mouvement.Description=reader.GetString(reader.GetOrdinal("Description"));
                String out_date = reader.GetString(reader.GetOrdinal("Date"));
                try
                {
                    DateTime dt = DateTime.ParseExact(out_date, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture);
                }
                catch (Exception ex) { }
                this.Mouvements.Add(mouvement);

            }
            sql_con.Close();
        }

The first method works, but the second gives me an exception near the try catch. 第一种方法有效,但是第二种方法使我在try catch附近出现异常。 Format are exactly the same. 格式完全相同。

此处的调试器捕获

Hope someone will be able to help me ! 希望有人能够帮助我!

If you want to use 24-hour time you need to use HH in your format string. 如果要使用24小时制,则需要在格式字符串中使用HH。 hh is for 12-hour time and the supplied time is > 12, so an exception is thrown. hh是12小时的时间,提供的时间> 12,因此将引发异常。 This will work. 这将起作用。

DateTime dt = DateTime.ParseExact(out_date, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

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

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