Make SQLite connection fail if database is missing? (deleted/moved)

前端 未结 6 593
终归单人心
终归单人心 2021-01-18 11:39

I have the following method inside class DBConnection. I call the method like this: SQLiteConnection conn = DBConnection.OpenDB(); when I want to o

6条回答
  •  日久生厌
    2021-01-18 12:20

    At least in System.Data.SQLite, you can add "FailIfMissing=True" to your connection string. SQLiteConnection.Open() will throw a SQLiteException if the database file does not exist.

    string ConnectString = "Data Source=file.sdb; FailIfMissing=True";
    DbConnection db = new SQLiteConnection(ConnectString);
    db.Open(); // Fails if file.sdb does not exist
    

    See SQLite Connection String Samples for another example, look for "Disable create database behaviour".

提交回复
热议问题