gpt4 book ai didi

c# - 为什么我得到 "No connection associated with this command"?

转载 作者:行者123 更新时间:2023-11-30 19:59:51 25 4
gpt4 key购买 nike

我的问题是我的数据读取器不工作。

这是我的代码:

SQLiteCommand comID = new SQLiteCommand("Select max(id) from haltestellen");
conSQLiteDb.Open();
SQLiteDataReader dr = comID.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
LblHaltestelleID1.Text = dr.GetValue(0).ToString();
}

最佳答案

只需使用适当的构造函数。将连接作为第二个参数的重载将您的命令关联到用于执行所需 sql 语句的连接。

 SQLiteCommand comID = new SQLiteCommand("Select max(id) from haltestellen", conSQLiteDb);
conSQLiteDb.Open();
SQLiteDataReader dr = comID.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
LblHaltestelleID1.Text = dr.GetValue(0).ToString();
}

您还可以使用命令属性连接

 SQLiteCommand comID = new SQLiteCommand("Select max(id) from haltestellen");
comID.Connection = conSQLiteDb;
conSQLiteDb.Open();

关于c# - 为什么我得到 "No connection associated with this command"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22685885/

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