1

my first time posting.

I made a console application that run in the background, it was written in C# and I'm using sqlite3 to store its information. It's running under mono but I'm getting unable to open database file after running fine for a few days. Restart the application solve the issue but it will give me the same error after few days.

I suspect it was the connection not getting dispose after close so I added dispose after the connection.close() but still getting the same error. I also did some search and find out this thread have similar problem, I did what he said but still getting same error.

Any help will be appreciated.

Sorry for bad english

UPDATE : My Code

private static void ExecuteNonQuery(string query)
    {
        if (isrunningundermono == true)
        {
            SqliteConnection sqlcon = new SqliteConnection(ConfigurationManager.AppSettings["connectionString"]);
            SqliteCommand sqlcmd;

            sqlcon.Open();
            sqlcmd = sqlcon.CreateCommand();
            sqlcmd.CommandText = query;
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
            sqlcmd.Dispose();
            sqlcon.Dispose();
        }
        else
        {
            SQLiteConnection sqlcon = new SQLiteConnection(ConfigurationManager.AppSettings["connectionString"]);
            SQLiteCommand sqlcmd;

            sqlcon.Open();
            sqlcmd = sqlcon.CreateCommand();
            sqlcmd.CommandText = query;
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
            sqlcmd.Dispose();
            sqlcon.Dispose();
        }
    }

    private static int ExecuteScalar(string query)
    {
        if (isrunningundermono == true)
        {
            SqliteConnection sqlcon = new SqliteConnection(ConfigurationManager.AppSettings["connectionString"]);
            SqliteCommand sqlcmd;

            sqlcon.Open();
            sqlcmd = sqlcon.CreateCommand();
            sqlcmd.CommandText = query;
            int total = Convert.ToInt32(sqlcmd.ExecuteScalar());
            sqlcon.Close();
            sqlcmd.Dispose();
            sqlcon.Dispose();

            return total;
        }
        else
        {
            SQLiteConnection sqlcon = new SQLiteConnection(ConfigurationManager.AppSettings["connectionString"]);
            SQLiteCommand sqlcmd;

            sqlcon.Open();
            sqlcmd = sqlcon.CreateCommand();
            sqlcmd.CommandText = query;
            int total = Convert.ToInt32(sqlcmd.ExecuteScalar());
            sqlcon.Close();
            sqlcmd.Dispose();
            sqlcon.Dispose();

            return total;
        }
    }

    private static DataTable ExecuteDataSet(string query)
    {
        if (isrunningundermono == true)
        {
            DataTable dt = new DataTable();
            SqliteConnection sqlcon = new SqliteConnection(ConfigurationManager.AppSettings["connectionString"]);
            SqliteCommand sqlcmd = new SqliteCommand(sqlcon);
            sqlcon.Open();
            sqlcmd.CommandText = query;
            SqliteDataReader reader = sqlcmd.ExecuteReader();
            dt.Load(reader);
            reader.Close();
            sqlcon.Close();
            sqlcmd.Dispose();
            sqlcon.Dispose();

            return dt;
        }
        else
        {
            DataTable dt = new DataTable();
            SQLiteConnection sqlcon = new SQLiteConnection(ConfigurationManager.AppSettings["connectionString"]);
            SQLiteCommand sqlcmd = new SQLiteCommand(sqlcon);
            sqlcon.Open();
            sqlcmd.CommandText = query;
            SQLiteDataReader reader = sqlcmd.ExecuteReader();
            dt.Load(reader);
            reader.Close();
            sqlcon.Close();
            sqlcmd.Dispose();
            sqlcon.Dispose();

            return dt;
        }
    }

    public static bool IsRunningOnMono()
    {
        return Type.GetType("Mono.Runtime") != null;
    }
2
  • Please show the code you use. Commented Jan 23, 2014 at 14:29
  • hi, i just update my post and added the code. Commented Jan 24, 2014 at 3:46

1 Answer 1

1

SQLiteConnection.Dispose internally just calls SQLiteConnection.Close, so you're still effectively closing the connection before the command, which I guess is the problem on Mono. I think if you just remove the explicit calls to sqlcon.Close you'd see a better result.

By the way, it's customary to employ the using statement when dealing with IDisposable instances, eg:

using (var conn = new SQLiteConnection(...))
{
  conn.Open();
  using (var cmd = new SQLiteCommand(conn))
  {
  }
}

I think it's natural to dispose of the command before the connection, since the command uses the connection (and the using statement helps to keep this straight), but I see no reason why the reverse would work on Windows but not on Mono...

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the answer, will give results in a few days.

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.