0

My project should display data from an sqlite3 database. The database name is db.sqlite3. The error is: Unable to open the database file

here is my code:

public partial class OverviewAdmin : System.Web.UI.Page
    {

        private SQLiteConnection sql_con;
        private SQLiteCommand sql_cmd;
        private SQLiteDataAdapter DB;
        private DataSet DS = new DataSet();
        private DataTable DT = new DataTable();


        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void SetConnection()
        {
            sql_con = new SQLiteConnection
                ("Data Source=db.sqlite3;");
        }

        private void LoadData()
        {
            SetConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            string CommandText = "select * from scotty_fahrt";
            DB = new SQLiteDataAdapter(CommandText, sql_con);
            DS.Reset();
            DB.Fill(DS);
            DT = DS.Tables[0];
            GVOutput.DataSource = DT;
            sql_con.Close();
        }

Maybe its because of the database file name.

1 Answer 1

3

Put your database file in the APP_DATA subfolder of your site and change the method that sets the connection to

    private void SetConnection()
    {
        sql_con = new SQLiteConnection
            ("Data Source=|DataDirectory|\\db.sqlite3;");
    }

By the way, I would remove the global variable for the connection and change your SetConnection to return the SQLiteConnection. This will allow to apply the using statement to correctly handle disposable like a connection also in case of exceptions

    private SQLite GetConnection()
    {
        return new SQLiteConnection("Data Source=|DataDirectory|\\db.sqlite3;");
    }

    private void LoadData()
    {
        using(SQLiteConnection cn = GetConnection())
        {
            cn.Open();
            string CommandText = "select * from scotty_fahrt";
            DB = new SQLiteDataAdapter(CommandText, sql_con);
            DS.Reset();
            DB.Fill(DS);
            DT = DS.Tables[0];
            GVOutput.DataSource = DT;
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I've git another question. The method with the sqlite getconnection. There i have to add the reference sqlite, but it doesnt' work. Did you now why?
Do you have added the references to the assembly in the project references? And do you have added the correct using statement to the beginning of files that contains references to SQLite classes?
Yeah i addaed SQLite ( also installed it ) and afterwards i wrote added the .SQLite; using Statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.