1

我已经搜索了几个小时了。我找不到任何对我的问题有帮助的东西。Read() 函数总是返回 false。如果我使用 sqlplus 运行 SQL 命令,我会得到以下结果:

   GERA_ID   GETY_BEZEICHNUNG
   ---------------------------
    100001   Blackberry
    100002   GSM

这是代码的简化版本:

List<Divice> divices = new List<Divice>();

using (OracleConnection connection = new OracleConnection(connectionString))
{
    OracleCommand cmd = new OracleCommand("select gera_id, gety_bezeichnung from idc_geraet, idc_geraettyp where idc_geraettyp.gety_id = idc_geraet.gety_id and pers_id = 4711");
    cmd.Connection = connection;

    connection.Open();
    OracleDataReader reader = cmd.ExecuteReader();
    while(reader.Read()) //returns always false
    {
        Divice g = new Divice();
        g.gera_id = reader.GetDecimal(0);
        g.gety_bezeichnung = reader.GetString(1);
        divices.Add(g);
    }
    reader.Close();
}

编辑:

之前调用了此代码。它工作正常:

IDC_PERSON p = new IDC_PERSON();
using (OracleConnection connection = new OracleConnection(connectionString))
    {
        OracleCommand cmd = new OracleCommand("select PERS_VNAME, PERS_NNAME, PERS_EINTRDATUM from idc_person where PERS_ID = 4711");
        cmd.Connection = connection;

        connection.Open();
        OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        if (reader.Read())
        {
            p.PERS_ID = user_id;
            p.PERS_VNAME = reader.GetString(0);
            p.PERS_NNAME = reader.GetString(1);
            p.PERS_EINTRDATUM = reader.GetDateTime(2);
        }
        reader.Close();
    }

所以,这里是整个代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Oracle.DataAccess.Client;
using TelKoOpt.Models;
using System.Data;

namespace TelKoOpt.Controllers
{
    public class HomeController : Controller
    {
        string connectionString = "user id=scott;password=tiger;" +
                "data source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)" +
                "(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)))";
        public ActionResult Index(int user_id)
        {
            MyDbContext dbcontext = new MyDbContext();
            IDC_PERSON p = new IDC_PERSON();
            List<IDC_GERAET> geraete = new List<IDC_GERAET>();
            List<TELGSMEGN> telgsmegn = new List<TELGSMEGN>();
            dbcontext.pers = p;
            dbcontext.geraete = geraete;
            dbcontext.telgsmegn = telgsmegn;

            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                OracleCommand cmd = new OracleCommand("select PERS_VNAME, PERS_NNAME, PERS_EINTRDATUM from idc_person where PERS_ID = " + user_id);
                //OracleCommand cmdTelg = new OracleCommand("select service, sum(betrag), sum(dauer), sum(anzahl), zonen from test_telgsmegn where gera_id = " + user_id + " and datumaktion between '" + "01.02.2012" + "' and '" + "20.03.2012" + "' group by service, zonen; ");
                cmd.Connection = connection;
                //cmdTelg.Connection = new OracleConnection(connectionString);

                try
                {
                    connection.Open();
                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        p.PERS_ID = user_id;
                        p.PERS_VNAME = reader.GetString(0);
                        p.PERS_NNAME = reader.GetString(1);
                        p.PERS_EINTRDATUM = reader.GetDateTime(2);
                    }
                    reader.Close();
                }
                catch (OracleException)
                {
                    //return View(dbcontext);
                }
            }
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                OracleCommand cmd = new OracleCommand("select gera_id, gety_bezeichnung from idc_geraet, idc_geraettyp where idc_geraettyp.gety_id = idc_geraet.gety_id and pers_id = " + user_id);
                cmd.Connection = connection;

                try
                {
                    connection.Open();
                    OracleDataReader reader = cmd.ExecuteReader();
                    while(reader.Read()) 
                    {
                        IDC_GERAET g = new IDC_GERAET();
                        g.gera_id = reader.GetDecimal(0);
                        g.gety_bezeichnung = reader.GetString(1);
                        geraete.Add(g);
                    }
                    reader.Close();
                }
                catch (OracleException)
                {
                    //return View(dbcontext);
                }
            }
            return View(dbcontext);
        }
    }
}
4

2 回答 2

0

这可能不是您的情况的答案,但我在调试代码时遇到了类似的问题,它可能会帮助遇到这个问题的其他人。

我想出的解决方案实际上只针对你调试的时候。单步执行代码时,如果您展开“结果视图”(枚举可枚举),当您到达代码中读取数据读取器的部分时,读取器将已经位于结果的末尾,并将返回错误的。这是因为数据读取器只是向前的,一旦您枚举到下一个结果(在本例中由调试器完成),您就无法返回。

于 2013-08-08T14:14:28.820 回答
-1

我面临同样的问题这就是我解决的方法

用尽型

 public void ExecuteStoredProcReturnDataReader(string sQueryName, out IDataReader dr, List<DBParam> oParams =null)
    {


        try
        {
            dbHelper DBProvider = new dbHelper();
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            oCmd = DBProvider.CreateCommand(sQueryName, conn);

            if (oParams !=null)
            DBProvider.CreateParameters(oParams, ref oCmd);

            dr = oCmd.ExecuteReader();




        }

        catch (Exception e)
        {

            rethrow = DataAccessExceptionHandler.HandleException(ref e);
            if (rethrow)
            {
                throw e;
            }
            dr = null;  
        }

    }
于 2014-05-25T16:12:35.793 回答