oracle 通过 OracleDataReader 读取时处理 NULL 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26024722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handle NULL values when reading through OracleDataReader?
提问by Analytic Lunatic
I'm working on my first ASP.Net application, and seem to be hitting a lot of hurdles (background primarily in WinForms with a recent MVC5 project under my belt).
我正在开发我的第一个 ASP.Net 应用程序,并且似乎遇到了很多障碍(主要是在 WinForms 中,最近有一个 MVC5 项目在我的腰带下)。
I am successfully making my DB connection using OracleCommand
and executing my query, but when I try reading through the rows I am getting a Column contains NULL value
on the second row for odr.GetDecimal(1)
. Anyone know how to handle null values when reading through an OracleDataReader?
我成功地使用OracleCommand
并执行我的查询建立了我的数据库连接,但是当我尝试通读行时,我Column contains NULL value
在第二行获得了odr.GetDecimal(1)
. 任何人都知道通过 OracleDataReader 读取时如何处理空值?
Below is my code:
下面是我的代码:
List<YearsOfService> yearsOfService = new List<YearsOfService>();
string SQL = "SELECT SCHOOL_YEAR as YEAR, " +
"TOTAL_SERVICE_CREDIT as ServiceCredited, " +
"RETIREMENT_SALARY as Salary, " +
"SOURCE_VALUE as CoveredEmployer " +
"FROM " + Schema + ".RANDOM_ORACLE_TABLE a " +
"WHERE MEMBER_ACCOUNT_ID = :memberAccountId";
DbConnection dbc = new DbConnection();
OracleCommand cmd = dbc.GetCommand(SQL);
cmd.Parameters.Add(new OracleParameter("memberAccountId", memberAccountId));
OracleDataReader odr = cmd.ExecuteReader();
int counter = 0;
if (odr.HasRows)
{
while (odr.Read())
{
YearsOfService yos = new YearsOfService();
yos.Year = odr.GetInt16(0);
yos.ServiceCredited = odr.GetDecimal(1); // Error on Second Pass
yos.Salary = odr.GetDecimal(2);
yos.CoveredEmployer = odr.GetString(3);
yearsOfService.Add(yos);
counter++;
}
}
return yearsOfService;
}
I had thought a simple check for NULL and if so replace with 0 (since expecting a Decimal
value) would work with the following, but no luck. Same error: yos.ServiceCredited = Convert.IsDBNull(odr.GetDecimal(1)) ? 0 : odr.GetDecimal(1);
.
我曾想过对 NULL 进行简单的检查,如果是这样,则替换为 0(因为需要一个Decimal
值)将适用于以下内容,但没有运气。同样的错误:yos.ServiceCredited = Convert.IsDBNull(odr.GetDecimal(1)) ? 0 : odr.GetDecimal(1);
。
Full error is:
完整的错误是:
An exception of type 'System.InvalidCastException' occurred in Oracle.DataAccess.dll but was not handled in user code
Oracle.DataAccess.dll 中发生了“System.InvalidCastException”类型的异常,但未在用户代码中处理
Additional information: Column contains NULL data
附加信息:列包含 NULL 数据
I have confirmed that my 2 rows being returned are in the following format:
我已确认我返回的 2 行采用以下格式:
Year|CreditedService|Salary |CoveredEmployer
2013|0.70128 |34949.66|ER
2014|NULL | 2213.99|NULL
Anyone have advice on how best to proceed? How should I handle receiving the NULL value when reading through my OracleDataReader?
任何人都有关于如何最好地进行的建议?在通过我的 OracleDataReader 读取时,我应该如何处理接收 NULL 值?
回答by Sourav 'Abhi' Mitra
yos.ServiceCredited = odr.IsDBNull(1) ? 0 : odr.GetDecimal(1);
OracleDataReader
provides a IsDBNull()
method.
OracleDataReader
提供了一种IsDBNull()
方法。
And the docs on GetDecimal()
ask us to do this
并且文档GetDecimal()
要求我们这样做
Call IsDBNull to check for null values before calling this method.
在调用此方法之前调用 IsDBNull 以检查空值。