我在我的代码中尝试多个查询,如下所示,但它在行MySqlDataReader rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader();
引发异常,因为我已经打开了rdr
我用Google搜索并发现@ according to MSDN不受支持,我需要有关如何修复它的指导?
public IEnumerable<BuildNotes_op> RetrieveBuildDetails(string BuildID)
{
....
var conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = AciDev.aciDevConnectionString;
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = @"query1";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader(); --> **rdr open here**
....
while (rdr.Read())
{
.....
if (rdr[4] != DBNull.Value)
....................
else
........
string sql_get_latest_build_notes_cmd = "query2";
MySqlCommand get_latest_build_notes_cmd = new MySqlCommand(sql_get_latest_build_notes_cmd, conn);
MySqlDataReader rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader(); --> **throws exception here**
if (rdr_get_latest_build_notes.HasRows)
{
....................
}
rdr_get_latest_build_notes.Close();
if (BuildDetails != null)
{
BuildNotesDetails.Add(BuildDetails);
}
}
rdr.Close();
conn.Close();
return BuildNotesDetails;
}
catch
{
throw;
}
finally
{
conn.Close();
}
例外:
There is already an open DataReader associated with this Connection which must be closed first.
答案 0 :(得分:1)
试试这个:
public IEnumerable<BuildNotes_op> RetrieveBuildDetails(string BuildID)
{
var conn = new MySql.Data.MySqlClient.MySqlConnection();
var conn2 = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = AciDev.aciDevConnectionString;
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = @"query1";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader(); --> **rdr open here**
....
while (rdr.Read())
{
.....
if (rdr[4] != DBNull.Value)
....................
else
........
string sql_get_latest_build_notes_cmd = "query2";
conn2.ConnectionString = AciDev.aciDevConnectionString;
Console.WriteLine("Connecting to MySQL...");
conn2.Open();
MySqlCommand get_latest_build_notes_cmd = new MySqlCommand(sql_get_latest_build_notes_cmd, conn2);
MySqlDataReader rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader(); --> **throws exception here**
if (rdr_get_latest_build_notes.HasRows)
{
....................
}
rdr_get_latest_build_notes.Close();
conn2.Close();
if (BuildDetails != null)
{
BuildNotesDetails.Add(BuildDetails);
}
}
rdr_get_latest_build_notes.Close();
rdr.Close();
conn.Close();
conn2.Close();
return BuildNotesDetails;
}
catch
{
throw;
}
finally
{
rdr_get_latest_build_notes.Close();
rdr.Close();
conn.Close();
conn2.Close();
}
创建第二个connection
var
答案 1 :(得分:0)
选项:
List<T>
或类似内容join
等一次性读取所有数据,作为单一广泛的结果集 - 并在客户端处理select
操作来检索多个网格中的数据,并在客户端处理它们