简体   繁体   中英

SQLite return wrong string value C#

I have local sqlite db with some values. And i have "name" and "descritption" columns with "string" type. In this cell of columns can be values like "32145152174524132151421". When i tries to get values using System.Data.SQLite library, in any cases using reader.GetValue().toString() or reader.GetString(), i got values like "32321541e+35" or InvalidCast exception, but DB stores normal values.

SQLite and his problems with CastValues just killing me. Can someone help me?

"Zakupki_name" and "Zakupki_description" have "string" data type.

I wrote it without a dot at the end, but in DB it saves with dot.

"Zakupki_name" Save in DB - 12323141276876900118036480. Returns by Code - 1.23231412768769e+25

"Zakupki_description" Save in DB - 123444441521613002768384. Returns by Code - 1.23444441521613e+23

Code for getting data from the database.

 SQLiteCommand command = new SQLiteCommand(@"SELECT Zakupki_id,
                                                       Zakupki_name,
                                                       Zakupki_description,
                                                       Zakupki_update_date,
                                                       Zakupki_value_limit
                                                       FROM Zakupki;",connection);
            SQLiteDataReader reader = command.ExecuteReader();
            int rowCount = GetRowCount(Tables.Zakupki);
            if (rowCount != 0)
            {
                zakupkis = new List<Zakupki>();
                while (reader.Read())
                {
                    zakupkis.Add(new Zakupki(reader.GetInt32(0), reader.GetValue(1).ToString(), reader.GetValue(2).ToString(), DateTime.Parse(reader.GetString(3)), double.Parse(reader.GetValue(4).ToString())));
                }
            }

Code for insert values to db.

public void InsertZakupku(string name, string description,double value_limit)
        {
            //Добавить закупку
            try
            {
                createConnection();
                SQLiteCommand command = new SQLiteCommand(string.Format(@"INSERT INTO Zakupki (
                                                                    Zakupki_name,
                                                                    Zakupki_description,
                                                                    Zakupki_update_date,
                                                                    Zakupki_value_limit)
                                                                    VALUES ('{0}','{1}','{2}','{3}');", name, description, DateTime.Now, value_limit), connection);
                command.ExecuteNonQuery();
                closeConnection();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                closeConnection();
            }

Using method to insert value.

sQLiteClient.InsertZakupku(zakupkiName.Text, zakupkiDescription.Text, double.Parse(zakupkiValue.Text));

通过使用varchar(255)而不是stringtext解决了这个问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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