有没有比这个更好的方法来检查从SQL Server数据库中获取的DataTable中的DataColumn是否为数字?
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetStoredProcCommand("Get_Some_Data");
DataSet ds = db.ExecuteDataSet(cmd);
foreach (DataTable tbl in ds.Tables) {
foreach (DataColumn col in tbl.Columns) {
if (col.DataType == typeof(System.Single)
|| col.DataType == typeof(System.Double)
|| col.DataType == typeof(System.Decimal)
|| col.DataType == typeof(System.Byte)
|| col.DataType == typeof(System.Int16)
|| col.DataType == typeof(System.Int32)
|| col.DataType == typeof(System.Int64)) {
// this column is numeric
} else {
// this column is not numeric
}
}
}
Array.IndexOf(numericTypes, col.DataType) != -1
。 - Dmytrii Nagirniakif (col.DataType.IsArray) return numericTypes.Contains(col.DataType.GetElementType());
例如,如果我有一个填充了来自查询 SQL Server 08 数据库的ODBCDataReader
结果的 DataTable,在服务器上作为timestamp
类型的列被转换为 DataTable 中的System.Byte[]
。 - p0lar_bear