gpt4 book ai didi

c# - SQLite 数据库未打开错误

转载 作者:行者123 更新时间:2023-11-30 20:22:47 24 4
gpt4 key购买 nike

我有一个从 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 语句之前一切似乎都工作正常:

            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

最佳答案

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

添加:SQLconn2.Open();

关于c# - SQLite 数据库未打开错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30468008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com