我不得不回到一个现有项目(几乎两个月大)进行一次小改动。我现在正在左右两侧获得例外,但它处于工作状态并部署到生产并正在运行。这些错误也很奇怪:现在所有Oracle数据类型的读取都不同。行的Number(10)di列很容易作为DataRow读取并保存到POCO各自的int属性中。但是同样的代码现在失败了。
之前正在使用的原始代码:
public static async Task<(OracleConnection connection, OracleDataAdapter adapter)> GetOraclePackage()
{
try
{
// To satisfy async. serves no purpose.
await Task.FromResult(0);
if (WebConfigurationManager.ConnectionStrings[Constants.ConnectionStringBilling] == null)
{
throw new ApplicationException("Oracle connection string is not found in web.config");
}
var oraConnection = new OracleConnection { ConnectionString = WebConfigurationManager.ConnectionStrings[Constants.ConnectionStringBilling].ConnectionString };
var oraDataAdapter = new OracleDataAdapter { SelectCommand = new OracleCommand { CommandType = CommandType.Text, Connection = oraConnection } };
return (oraConnection, oraDataAdapter);
}
catch (OracleException oraExp)
{
throw new RepositoryLayerException("Oracle error in repository's helper method GetOraclePackage() " + oraExp.Message, oraExp);
}
catch (Exception e)
{
throw new RepositoryLayerException("Error in repository's helper method GetOraclePackage() " + e.Message, e);
}
}
public async Task<IList<BDO.AirportServiceWithTerminal>> GetAllServices()
{
var objResult = new List<BDO.AirportServiceWithTerminal>();
var oraclePackage = await Helper.GetOraclePackage();
var ds = new DataSet();
try
{
var sql = @"
SELECT
s.service_code,
s.account_code,
s.service_fees,
s.customer_type,
s.period,
s.ser_name_e,
s.isvat,
s.vatvalue
FROM
serviceinfo s
WHERE
sevice_status = '1'
AND
service_fees > 1
AND
account_code IN (
'AB','BC','CD','DE'
)
";
oraclePackage.connection.Open();
oraclePackage.adapter.SelectCommand.CommandText = sql;
oraclePackage.adapter.Fill(ds, "ServiceList");
if (ds.Tables[0] != null)
{
foreach (DataRow oneService in ds.Tables[0].Rows)
{
var newTerminalService = new BDO.AirportServiceWithTerminal
{
Code = oneService.Field<int>("service_code"),
NameEnglish = oneService.Field<string>("ser_name_e").Replace(@"""", "").Trim(),
Fee = oneService.Field<decimal>("service_fees"),
IsVat = oneService.Field<string>("isvat") == "1",
VatValue = int.Parse(oneService.Field<string>("vatvalue"))
};
objResult.Add(newTerminalService);
}
}
}
catch (Exception exp)
{
throw new RepositoryLayerException("An error occurred while retrieving all services from billing system", exp);
}
finally
{
oraclePackage.adapter.Dispose();
if (oraclePackage.connection.State == ConnectionState.Open) oraclePackage.connection.Close();
oraclePackage.connection.Dispose();
}
return objResult;
}
现在,当它试图将service_code
强制转换为int时,它会失败。我试着得到service_code
的类型如下:
oneService["service_code"].GetType()
我得到UnderlyingSystemType: {Name = "Int64" FullName = "System.Int64"}
或UnderlyingSystemType: {Name = "Decimal" FullName = "System.Decimal"}
(写这个问题,我开始得到十进制)
通过做一些演员来解决这个问题,因为我知道数据库没有被更改,我不能接受新类型,因为有很多。同样的情况是小数现在报告为双打。
答案 0 :(得分:1)
在一般情况下 Oracle的Number(10)
不适合int
(Int32
),因为Number(10)
的范围更广{ EM>:
Type Min Value Max Value
------------------------------------
Number(10) -9999999999 .. 9999999999
Int32 -2147483648 .. 2147483647
这就是为什么,例如9876543210
可以表示为Number(10)
而不是int
(这就是.Net将Number(10)
映射到{{的原因1}})。如果数据库中的值保证足够小(因此实际上适合 Int64
),请尝试使用int
:
Convert