데이터베이스 C # SQLite에 레코드를 삽입 할 수 없습니다.

Nov 23 2020

나는 데이터베이스와 관련된 오류로 어려움을 겪고 있습니다. 기본적으로 SQLite db가 있고 데이터를 삽입하고 싶지만 메서드를 실행 한 후에는 데이터가 db에 기록되지 않지만 오류도 표시되지 않습니다. 이것은 내 코드입니다. 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();
        }

    }

및 삽입 방법

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);
            }
        }

또한 데이터베이스 옵션을 다음과 같이 설정했습니다.

Build Action: Content
Copy to output directory: Copy always

답변

2 DmitriyGrebennikov Nov 23 2020 at 15:58

이 명령을 실행하는 것을 잊었습니다.

sqLiteCommand.ExecuteNonQuery();

문서를 참조하십시오 : ExecuteNonQuery