我有一个 sybase 数据库,它可以正确获取查询结果,如下所示......
select
S.ipoInternalID,
clientAccount,
clientPrice,
clientAccountType,
interestOnLoan =
CASE WHEN useHIBOR = 1 then
ROUND(financingAmount * (fixedRate + spreadRate) *
I.noOfDaysForInterest/365/100,2)
ELSE
ROUND(financingAmount * (I.fundingRate+ spreadRate) *
I.noOfDaysForInterest/365/100,2) END,
useHIBORSTR =
CASE WHEN useHIBOR = 1 then
"LOCK-IN RATE + SPREAD"
ELSE
"COST OF FUNDING + SPREAD" END,
from subscription S, iPO I, allocation A
where
S.ipoInternalID = @ipoInternalID and
I.ipoInternalID = @ipoInternalID and
A.ipoInternalID = @ipoInternalID and
S.ccassID *= A.ccassID
order by S.ccassID
interestOnLoan
请注意上面计算字段的方式。
现在,当我在 SQL Advantage 工具中运行此查询时,它运行良好并为我提供了interestOnLoan
. 当我使用通过 OleDB 加载此查询的 .Net 1.1 API 运行此查询时,它运行良好...
myCommand.CommandText = myQuery;
myAdapter.SelectCommand = myCommand;
int i = myAdapter.Fill(resultSet);
我的结果集填充正常。
但是当我在 .net 4.0 中执行上述代码时,结果集错误为
“对于 Decimal,值要么太大要么太小。”
它存在问题的值是因为我还通过如下方式interestOnLoan
执行了命令...IDataReader
using (var dr = myCommand.ExecuteReader())
{
resultSet.Tables.Add(ConvertDataReaderToTableManually(dr));
}
private static DataTable ConvertDataReaderToTableManually(IDataReader dr) {
var dt = new DataTable();
var dtSchema = dr.GetSchemaTable();
var listCols = new List<DataColumn>();
if (dtSchema != null) {
foreach (DataRow drow in dtSchema.Rows) {
var columnName = Convert.ToString(drow["ColumnName"]);
var t = (Type) (drow["DataType"]);
var column = new DataColumn(columnName, t);
column.Unique = (bool) drow["IsUnique"];
column.AllowDBNull = (bool) drow["AllowDBNull"];
column.AutoIncrement = (bool) drow["IsAutoIncrement"];
listCols.Add(column);
dt.Columns.Add(column);
}
}
// Read rows from DataReader and populate the DataTable
int j = 0;
while (dr.Read()) {
j++;
var dataRow = dt.NewRow();
for (int i = 0; i < listCols.Count; i++) {
try {
dataRow[((DataColumn)listCols[i])] = dr[i];
} catch (Exception ex1) { }
}
dt.Rows.Add(dataRow);
}
return dt;
}
在这里,它在从 dr[i] 读取dataRow[((DataColumn)listCols[i])] = dr[i]
问题的地方出错;
观察到i
第 th 列不过是interestOnLoan。
所以不知何故.Net 4.0 无法读取这个值。它可以正确读取其他十进制值,例如clientPrice
.
为什么会出现这种情况......
另外我想问一下,有什么方法可以默认加载DataReader
as Double
(而不是Decimal
)中的值?