简体   繁体   中英

Resetting OracleDataReader reader and calling SQL inside C# foreach loop

How can I reset a dataReader inside a foreach loop? I am trying to run a select query inside a loop but reader is always having the records from the first select. I tried reader.Dispose() and reader.Close() methods but still the value is not resetting to contain the next set of data. here is my code sample:

           using (OracleCommand cmd = con.CreateCommand())
            {
              

                if (getqMidLid != null)
                {
                    foreach (var item in getqMidLid)
                    {

                        cmd.BindByName = true;
                        cmd.InitialLONGFetchSize = -1;

                        try
                        {
                          
                            cmd.CommandText = "select Question_mid, " +
                                "               question_lid," +
                                "               max_score," +
                                "               actual_score," +
                                "               topic," +
                                "               answer_full" +
                                "                from QnA where Question_mid = :mid and Question_lid = :lid" +
                                "                and (max_score = actual_score)";

                            OracleParameter mid = new OracleParameter("mid", item.QMID);
                            OracleParameter lid = new OracleParameter("lid", item.QLID);
                            cmd.Parameters.Add(mid);
                            cmd.Parameters.Add(lid);

                            using (OracleDataReader reader = cmd.ExecuteReader())
                            {
                                if (reader.HasRows)
                                {
                                    while (reader.Read())
                                    {
                                        qna.Add(new QuestionAnswer
                                        {
                                            QMID = reader.GetInt32(0),
                                            QLID = reader.GetInt32(1),
                                            MaxScore = reader.GetInt16(2),
                                            ActualScore = reader.GetInt16(3),
                                            Topic = reader.GetString(4),
                                            AnswerFull = reader.GetString(5)
                                        });
                                    }
                                    }
                            }


                            

                            
                        }
                        catch (Exception sql)
                        {
                            Console.WriteLine(sql.Message);
                            throw;
                        }
                    
                   }
                }


            }

Solved it by moving the OracleCommand object inside the result loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM