mardi 26 mai 2015

SQLite Database is not open Error

I have a constructor that takes data from a SQL Server database and puts it in a local SQLite database:

public ForemanController()
        {
            connectionString.DataSource = "dxdb02v";
            connectionString.InitialCatalog = "QTRAX4619410";
            connectionString.UserID = "tunnelld";
            connectionString.Password = "david";

            string queryString = "SELECT * FROM [QTRAXAdmin].[vwQT_Foreman]";

            List<Foreman> list;
            // Creates a SQL connection
            using (var connection = new SqlConnection(connectionString.ToString()))
            {
                using (var command = new SqlCommand(queryString, connection))
                {
                    connection.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        list = new List<Foreman>();
                        while (reader.Read())
                        {
                            list.Add(new Foreman { ForeBadge = reader.GetString(0), ForeName = reader.GetString(1) });
                        }
                    }
                }
                connection.Close();
                allForeman = list.ToArray();
            }
            string deleteSQL = "DELETE FROM Foreman;";
            using (SQLiteConnection SQLconn1 = new SQLiteConnection(SQLiteConnectionString))
            {
                using (var command = new SQLiteCommand(deleteSQL, SQLconn1))
                {
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                }
            }

            using (SQLiteConnection SQLconn2 = new SQLiteConnection(SQLiteConnectionString))
            {
                SQLiteCommand cmd2 = SQLconn2.CreateCommand();
                foreach (Foreman row in allForeman)
                {
                    cmd2.CommandText = "INSERT INTO Foreman (ForeBadge, ForeName) VALUES (@param1, @param2);";
                    cmd2.Parameters.Add(new SQLiteParameter("@param1", row.ForeBadge));
                    cmd2.Parameters.Add(new SQLiteParameter("@param2", row.ForeName));
                    cmd2.ExecuteNonQuery();
                }
            }
        }

Everything seems to be working fine until the last using statement:

            using (SQLiteConnection SQLconn2 = new SQLiteConnection(SQLiteConnectionString))
            {
                SQLiteCommand cmd2 = SQLconn2.CreateCommand();
                foreach (Foreman row in allForeman)
                {
                    cmd2.CommandText = "INSERT INTO Foreman (ForeBadge, ForeName) VALUES (@param1, @param2);";
                    cmd2.Parameters.Add(new SQLiteParameter("@param1", row.ForeBadge));
                    cmd2.Parameters.Add(new SQLiteParameter("@param2", row.ForeName));
                    cmd2.ExecuteNonQuery();
                }
            }

I'm getting this error:

enter image description here

Aucun commentaire:

Enregistrer un commentaire