【问题标题】:Why I get "No connection associated with this command"?为什么我得到“没有与此命令关联的连接”?
【发布时间】:2014-03-27 11:13:31
【问题描述】:
我的问题是我的数据阅读器不工作。
这是我的代码:
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();
}
【问题讨论】:
标签:
c#
asp.net
sqldatareader
【解决方案1】:
只需使用适当的构造函数。
将连接作为第二个参数的重载将您的命令与用于执行所需 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();
}
你也可以使用命令属性 Connection
SQLiteCommand comID = new SQLiteCommand("Select max(id) from haltestellen");
comID.Connection = conSQLiteDb;
conSQLiteDb.Open();