Start SQLite connection when there is no database? (Deleted / moved)

I have the following method inside class DBConnection. I call the method as follows: SQLiteConnection conn = DBConnection.OpenDB();when I want to open a connection so that I can fulfill my requests. I can call a similar method when I want to close the connection.

Method:

public static SQLiteConnection OpenDB()
{
    try
    {
        //Gets connectionstring from app.config
        string myConnectString =
            ConfigurationManager.ConnectionStrings[
                "LegMedSQLLite.Properties.Settings.LegMedSQLLiteDBConnectionString"].ConnectionString;

        var conn = new SQLiteConnection(myConnectString);

        conn.Open();
        return conn;
    }
    catch (SQLiteException e)
    {
        MessageBox.Show(e.ToString(), "TEST");
        return null;
    }
}

All this works great and dandy. However, the problem is trying to capture. Imagine the following scenario:

  • The database file has been moved / deleted.

. , , , - - , (), . , , SQLite . , , SQLite , , .

, , , - ( , , ..), SQLiteConnection conn = DBConnection.OpenDB();.

, File.Exists , . ?

+5
6

, System.Data.SQLite FailIfMissing=True " . SQLiteConnection.Open() SQLiteException, .

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

. SQLite Connection , " ".

+17

SQLite, .

try, Select top 1 * From Table , , conn. , .

+2

,

pragma integrity_check;

pragma quick_check; ( , )

"ok".

, .

+1

sqlite : , 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

, #

0

. SQLiteConnection IDisposable, , Dispose, .

-1

SQLite , File.Exists. , , , , catch.

-2

All Articles