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

前端 未结 6 520
终归单人心
终归单人心 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:19

    If there is no way to change the default SQLite behavior, then you might have to do a File.Exists. That would be better than connecting and creating a new file, checking to see if it's the database you want, then deleting the new file in the catch block.

    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2021-01-18 12:30

    For sqlite use this: Suppose you have connection string in textbox txtConnSqlite

         Using conn As New System.Data.SQLite.SQLiteConnection(txtConnSqlite.Text)
                Dim FirstIndex As Int32 = txtConnSqlite.Text.IndexOf("Data Source=")
                If FirstIndex = -1 Then MsgBox("ConnectionString is incorrect", MsgBoxStyle.Exclamation, "Sqlite") : Exit Sub
                Dim SecondIndex As Int32 = txtConnSqlite.Text.IndexOf("Version=")
                If SecondIndex = -1 Then MsgBox("ConnectionString is incorrect", MsgBoxStyle.Exclamation, "Sqlite") : Exit Sub
                Dim FilePath As String = txtConnSqlite.Text.Substring(FirstIndex + 12, SecondIndex - FirstIndex - 13)
                If Not IO.File.Exists(FilePath) Then MsgBox("Database file not found", MsgBoxStyle.Exclamation, "Sqlite") : Exit Sub
                Try
                    conn.Open()
                    Dim cmd As New System.Data.SQLite.SQLiteCommand("SELECT * FROM sqlite_master WHERE type='table';", conn)
                    Dim reader As System.Data.SQLite.SQLiteDataReader
                    cmd.ExecuteReader()
                    MsgBox("Success", MsgBoxStyle.Information, "Sqlite")
                Catch ex As Exception
                    MsgBox("Connection fail", MsgBoxStyle.Exclamation, "Sqlite")
                End Try
             End Using
    

    I think you can easilly convert it to c# code

    0 讨论(0)
  • 2021-01-18 12:33

    Don't catch at that level. Instead, SQLiteConnection should implement IDisposable, meaning you should just return the open connection and allow calling code to handle any exceptions, as well as rely on the Dispose method to close the connection.

    0 讨论(0)
  • 2021-01-18 12:35

    I haven't used SQLite but that is pretty bizarre behaviour to auto create a brand new database.

    You could just adjust your try block to do a Select top 1 * From Table immediately after you open the connection, if it works, throw away the result and continue to return your conn object. If it fails, the exception handler should fire.

    0 讨论(0)
  • 2021-01-18 12:36

    If you want to detect database corruption issues on start up , you can execute the command

    pragma integrity_check;

    or

    pragma quick_check; ( which is faster, but less thorough )

    This returns a single row with the value "ok".

    Otherwise it will report errors that it encounters.

    0 讨论(0)
提交回复
热议问题