Home > database >  Disable SQLite automatic Database creation - C#
Disable SQLite automatic Database creation - C#

Time:04-28

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

CodePudding user response:

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();
            }
  • Related