C#-处理数据库类型可以是十进制或双精度
我使用以下代码将数据从数据库导入到表中:C#-处理数据库类型可以是十进制或双精度,c#,C#,我使用以下代码将数据从数据库导入到表中: public double[,] toValueArray(string fieldPrefix, int firstIndex, int lastIndex) { // return a bunch of value columns as a double array double[,] outArr = new double[TableRowCount,lastIndex-firstIn
public double[,] toValueArray(string fieldPrefix, int firstIndex, int lastIndex)
{
// return a bunch of value columns as a double array
double[,] outArr = new double[TableRowCount,lastIndex-firstIndex+1];
for (int i = firstIndex; i <= lastIndex; i++)
{
var col = TheDataTable.Columns[fieldPrefix + i];
int r = -1;
foreach (DataRow row in TheDataTable.Rows)
{
outArr[++r, i - firstIndex] = (row[col] == null || row[col] == DBNull.Value) ? 0 : (double)row[col];
}
}
return outArr;
}
public double[,]toValueArray(字符串字段前缀,int-firstIndex,int-lastIndex)
{
//以双数组形式返回一组值列
double[,]outArr=新的double[TableRowCount,lastIndex-firstIndex+1];
对于(int i=firstIndex;i首先,使用0D
获得一个双精度值,并避免将int
隐式转换为double
。可以尝试使用ToDouble
静态方法从Convert
类型进行转换。对于示例:
double setValue = 0D;
if (row[col] != null && row[col] != DBNull.Value)))
setValue = (row[col] is decimal) ? Convert.ToDouble(row[col]) : (double) row[col];
outArr[++r, i - firstIndex] = setValue;