mardi 26 avril 2016

Using System.Data.SQLite.dll in C# Error in Data Fill: Cannot access a disposed object

I was trying to create a generalized C# code for database management. So far the code is working fine for MSSQL and MySQL. But when i try to use it for SQLite it throws error.

I figured out the problem but i am unable to change my framework at this moment. I raised a ticket at SQLite Site asking for help but i think those chicken heads deleted it without even sending a mail saying they won't do it. The UUID of the ticket is 2747b8a677ae2057a5617df1e20684256d6bbc93.

A test code is written in VS 2010 in C# with .Net 4 Framework to recreate the error. It's a windows form application the full project source is uploaded here.

The same code is listed below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Data.SQLite;

namespace SQLite_Bug
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // We assume database already exist.
        // Need to replace **** with Appropriate values

        string ConStr_MSSQL = @"Persist Security Info=True;User ID=sa;Password=****;Initial Catalog=master;Data Source=****";
        string ConStr_MySQL = @"server=localhost;userid=root;password=****;database=****";
        string ConStr_SQLite = @"Data Source=E:\TestDB.sqllite;Version=3;FailIfMissing=true;Max Pool Size=100;Journal Mode=Off;";

        private void button1_Click(object sender, EventArgs e)
        {
            // MSSQL Test
            SqlConnection Con = new SqlConnection(ConStr_MSSQL);
            Con.Open();
            SqlCommand SC = Con.CreateCommand();
            SqlDataAdapter DA = new SqlDataAdapter();
            DataTable DT = new DataTable();

            SC.CommandTimeout = 0;
            SC.CommandType = CommandType.Text;
            SC.Connection = Con;
            SC.CommandText = "Select 'Test MSSQL Data'";

            DA.SelectCommand = SC;
            SC.Dispose();

            DA.Fill(DT);
            DA.Dispose();

            Con.Close();
            Con.Dispose();

            MessageBox.Show(DT.Rows[0][0].ToString());
            DT.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // MySQL Test
            MySqlConnection Con = new MySqlConnection(ConStr_MySQL);
            Con.Open();
            MySqlCommand SC = Con.CreateCommand();
            MySqlDataAdapter DA = new MySqlDataAdapter();
            DataTable DT = new DataTable();

            SC.CommandTimeout = 0;
            SC.CommandType = CommandType.Text;
            SC.Connection = Con;
            SC.CommandText = "Select 'Test MySQL Data'";

            DA.SelectCommand = SC;
            SC.Dispose();

            DA.Fill(DT);
            DA.Dispose();

            Con.Close();
            Con.Dispose();

            MessageBox.Show(DT.Rows[0][0].ToString());
            DT.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // SQLite Test
            //SQLiteConnection.CreateFile("E:\\TestDB.sqllite");
            SQLiteConnection Con = new SQLiteConnection(ConStr_SQLite);
            Con.Open(); // <-- Throw error almost every time. unable to open database file Error. Code 14.
            // 100% chance of getting this error if program crash before connection is closed.
            SQLiteCommand SC = Con.CreateCommand();
            SQLiteDataAdapter DA = new SQLiteDataAdapter();
            DataTable DT = new DataTable();

            SC.CommandTimeout = 0;
            SC.CommandType = CommandType.Text;
            SC.Connection = Con;
            SC.CommandText = "Select 'Test SQLite Data'";

            DA.SelectCommand = SC;
            SC.Dispose(); // I can't move it from here. :(

            DA.Fill(DT); // <-- Throw Error. Cannot access a disposed object. Object name: 'SQLiteCommand'.
            DA.Dispose();

            Con.Close();
            Con.Dispose();

            MessageBox.Show(DT.Rows[0][0].ToString());
            DT.Dispose();
        }
    }
}

Can you figure out why it happen just for SQLlite? I am sorry if there is any mistakes. And i having some network problem so i may not be able to replay right away :(

Thanks in advance :)

Aucun commentaire:

Enregistrer un commentaire