我正在使用 sql 连接来访问我的数据库中的不同表。但是代码返回了以下错误。
错误:“已经有一个打开的 DataReader 与此命令关联,必须先关闭”:
MyContext conn = new MyContext()
protected void ChangeName(int id)
{
User user = conn.MyOtherTable.First(x => x.id == id);
var elements = conn.MyTable.Where(x => x.id == id && x.name == name).OrderBy(x => x.id).OrderBy(x => x.name).
.Select(t => new { t.id, t.name, }).GroupBy(t => new { t.id, t.name, });
foreach (var item in elements)
{
foreach (var row in item)
{
for (int j = 1; j <= 5; j++)
{
if (row.name == "name")
{
user.name1 = row.name;
conn.SaveChanges();
}
if (row.name == "name2")
{
user.name2 = row.name;
conn.SaveChanges();
}
}
}
}
}