SQLite数据库未打开错误

时间:2015-05-26 20:12:19

标签: c# sql sqlite

我有一个构造函数,它从SQL Server数据库获取数据并将其放在本地SQLite数据库中:

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

在最后一次使用声明之前,一切似乎都正常工作:

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

我收到此错误:

enter image description here

2 个答案:

答案 0 :(得分:4)

那是因为那是你忘记打开连接的唯一地方。

添加:SQLconn2.Open();

答案 1 :(得分:1)

您忘了打开连接。

{MyString}/{Date1}/{ToDate}