我已经搜索了几个小时了。我找不到任何对我的问题有帮助的东西。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);
}
}
}