Nie można wstawić rekordu do bazy danych C # SQLite

Nov 23 2020

Zmagałem się z błędem związanym z bazą danych. Zasadniczo mam bazę danych SQLite i chcę wstawić dane, ale po wykonaniu metody żadne dane nie są zapisywane w bazie danych, ale nie są również wyświetlane żadne błędy. To jest mój kod: Klasa połączenia db:

class SqlDb
    {
        public static SQLiteConnection SqLiteConnection;
        public static string DATABASE_WAREHOUSE = "DaneDB.db";

        public static string DATABASE_LACZKA =
            $"Data Source= {DATABASE_WAREHOUSE};Version=3;New=False;Compress=True;"; public SQLiteConnection Connect() { try { SqLiteConnection = new SQLiteConnection(DATABASE_LACZKA); SqLiteConnection.Open(); } catch (Exception e) { MessageBox.Show($"Could not connect to the database {e}");
                throw;
            }

            return SqLiteConnection;
        }

        public void Disconnect()
        {
            SqLiteConnection.Close();
        }

    }

i metodę wstawiania

private void LoadToDb_Click(object sender, EventArgs e)
        {
            Data modelData = new Data();
            SqlDb db = new SqlDb();
            SQLiteConnection sqLiteConnection = db.Connect();
            SQLiteCommand sqLiteCommand = sqLiteConnection.CreateCommand();


            try
            {
                modelData.Name = firstName.Text;
                modelData.Age = Convert.ToInt32(DisplayAge.Text);
                modelData.LastName = LastName.Text;

                sqLiteCommand.CommandText =
                    $"insert into DANE values('{modelData.Name}', '{modelData.LastName}', '{modelData.Age}')"; 

                    db.Disconnect();

            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

Mam też opcje bazy danych ustawione na:

Build Action: Content
Copy to output directory: Copy always

Odpowiedzi

2 DmitriyGrebennikov Nov 23 2020 at 15:58

Zapomniałeś wykonać to polecenie:

sqLiteCommand.ExecuteNonQuery();

Zapoznaj się z dokumentacją: ExecuteNonQuery