禁用SQLITE自动数据库创建-C#

发布于 2025-01-23 13:38:33 字数 1027 浏览 0 评论 0原文

我成功使用一种尝试 - 捕获方法来测试C#与通用数据库的连接。 但是,在SQLite的情况下,当未检测到数据库时,它会创建一个新的,这在我的情况下是不需要的。 “当您连接到不存在的SQLITE数据库文件时,SQLITE会自动为您创建新数据库

SQLiteDataAdapter ad;
        DataTable dt = new DataTable();
        try
        {
            sqlite = new SQLiteConnection("Data Source=C:/Users/SKUL/Desktop/db/PatHist.db");
            SQLiteCommand cmd;
            sqlite.Open();  //Initiate connection to the db
            cmd = sqlite.CreateCommand();
            //-----------------
          
            picConnectionStatus.Image = Properties.Resources.icons8_database_view_64;
            lblConnectionEstablished.Text = "Connection Established";

            sqlite.Close();
           
        }
        catch (Exception ex)
        {
            picConnectionStatus.Image = Properties.Resources.icons8_delete_database_64;
            lblConnectionEstablished.Text = "Connection Error";
            lblConnectionEstablished.ForeColor = System.Drawing.Color.Red;
           
        }

I use successfully a try - catch method to test connection of C# with common databases.
However, in case of SQLite, when the database is not detected, it creates a new one, which is unwanted in my case. "When you connect to an SQLite database file that does not exist, SQLite automatically creates the new database for you"

SQLiteDataAdapter ad;
        DataTable dt = new DataTable();
        try
        {
            sqlite = new SQLiteConnection("Data Source=C:/Users/SKUL/Desktop/db/PatHist.db");
            SQLiteCommand cmd;
            sqlite.Open();  //Initiate connection to the db
            cmd = sqlite.CreateCommand();
            //-----------------
          
            picConnectionStatus.Image = Properties.Resources.icons8_database_view_64;
            lblConnectionEstablished.Text = "Connection Established";

            sqlite.Close();
           
        }
        catch (Exception ex)
        {
            picConnectionStatus.Image = Properties.Resources.icons8_delete_database_64;
            lblConnectionEstablished.Text = "Connection Error";
            lblConnectionEstablished.ForeColor = System.Drawing.Color.Red;
           
        }

What is needed is to disable some way the automatic database creating and trigger the "catch" routine

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2025-01-30 13:38:33

在@rafael的建议之后,我检查了文件的存在。解决这个问题。

if (System.IO.File.Exists(@"C:\Users\SKUL\Desktop\db\nPatHist.db"))
            {
               sqlite = new SQLiteConnection("Data Source=C:\\Users\\SKUL\\Desktop\\db\\PatHist.db");
                SQLiteCommand cmd;
                sqlite.Open();  //Initiate connection to the db
                cmd = sqlite.CreateCommand();
                //-----------------

                picConnectionStatus.Image = Properties.Resources.icons8_database_view_64;
                lblConnectionEstablished.Text = "Connection Established";


                sqlite.Close();
            }

after the suggestion of @Rafael i check the file presence. Doing this problem solved.

if (System.IO.File.Exists(@"C:\Users\SKUL\Desktop\db\nPatHist.db"))
            {
               sqlite = new SQLiteConnection("Data Source=C:\\Users\\SKUL\\Desktop\\db\\PatHist.db");
                SQLiteCommand cmd;
                sqlite.Open();  //Initiate connection to the db
                cmd = sqlite.CreateCommand();
                //-----------------

                picConnectionStatus.Image = Properties.Resources.icons8_database_view_64;
                lblConnectionEstablished.Text = "Connection Established";


                sqlite.Close();
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文