Hello. I am writing this code:

NpgsqlConnection DBconnection = _dbconnection; NpgsqlCommand DBcommand_1 = DBconnection.CreateCommand(); NpgsqlCommand DBcommand_2 = DBconnection.CreateCommand(); NpgsqlDataReader DataReader_1, DataReader_2; DBcommand_1.CommandText = "SELECT Что-то там FROM Откуда-то"; DataReader_1 = DBcommand_1.ExecuteReader(); int fields = DataReader_1.FieldCount; string[] product; string code; while (DataReader_1.Read()) { product = new string[fields + 3]; product[0] = DataReader_1.GetInt32(0).ToString(); code = DataReader_1.GetInt32(1).ToString(); DBcommand_2.CommandText = "SELECT Что-то там FROM Откуда-то WHERE Код = " + code; DataReader_2 = DBcommand_2.ExecuteReader(); ... ... ... } 

During the execution of the second request, an error is displayed: "There is already an open DataReader associated with this command. Why so, is DBcommand_1 and DBcommand_2 somehow connected? If OleDb is used instead of Npgsql, this error does not occur.

    1 answer 1

    Try to set the connection string MultipleActiveResultSets=true

    If you believe this answer , this is due to the fact that you are iterating with a DataReader'ом and one more query within one connection.

    If what does not help above, then launching a query in a separate connection will definitely help.

    • and what should the connection string look like with MultipleActiveResultSets = true? Can you give an example, please - Vaycheslav
    • I had this line "Server = 127.0.0.1; Port = 5432; User Id = postgres; Password = ************; Database = MyDataBase;", and where I will not add "MultipleActiveResultSets = true "an error occurs: key = value argument incorrect in ConnectionString. - Vaycheslav