让SQLite的连接失败,如果数据库丢失? (删除/移动) [英] Make SQLite connection fail if database is missing? (deleted/moved)

查看:899
本文介绍了让SQLite的连接失败,如果数据库丢失? (删除/移动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类DBConnection的里面下面的方法。我把这样的方法: SQLiteConnection康恩= DBConnection.OpenDB(); 时,我想打开一个连接,这样我就可以执行我的查询。我可以调用类似的方法时,我想关闭连接

I have the following method inside class DBConnection. I call the method like this: SQLiteConnection conn = DBConnection.OpenDB(); when I want to open an connection, so that I can execute my queries. I can call a similar method when I want to close the connection.

方法:

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

这一切工作正常,很正常。问题是在try-catch虽然。让我们想象一下以下情形:

This all works fine and dandy. The problem is the try-catch though. Let us imagine the following scenario:


  • 数据库文件已经
    移动/删除

的异常不会被抛出。其实,第一次抓我偶然发现是,当我执行我的第一个查询 - 它数字,有没有这样的表(S)和它抛出自己的异常。
我被这怪异的现象惊呆了,但我很快就发现了SQLite的创建一个新的
数据库。通过空是平均的没有表,没什么,只是名称相同,将其认为是有旧的数据库SQLite数据库文件。

The exception will never be thrown. Actually, the first catch I stumble upon is when I execute my first query - where it figures that there is no such table(s) and it throws its own exception. I was stunned by this weird phenomenon, but I soon found out that SQLite creates a new empty database. By empty is mean no tables, nothing, just an SQLite database file with the same name as the old database which was supposed to be there.

这是一个问题,我想我尝试调用 SQLiteConnection康恩= DBConnection的应用程序知道是否有错(没找到,损坏,被另一个进程等数据库)尽快。 OpenDB();

This is an issue, I want the application to know if there is something wrong (database not found, corrupted, being used by another process etc.) as soon as I try to call SQLiteConnection conn = DBConnection.OpenDB();.

当然,我可以尝试在我的方法调用File.Exists,但是这似乎并不像一个适当的解。任何帮助吗?

Naturally, I could try call a File.Exists in my method, but that doesn't seem like a proper solution. Any help?

推荐答案

至少在System.Data.SQLite,您可以添加 FailIfMissing = TRUE 你的连接字符串。 SQLiteConnection.Open()将抛出一个 SQLiteException 如果数据库文件不存在。

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

请参阅 SQLite的连接字符串示例的另一个例子,寻找禁用创建数据库的行为。

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

这篇关于让SQLite的连接失败,如果数据库丢失? (删除/移动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆