确定是否是DataColumn的数字 [英] Determine if DataColumn is numeric
问题描述
有没有比这更好的方式来检查,如果在一个DataTable中的一个DataColumn是数字(从SQL Server数据库来)?
数据库DB = DatabaseFactory.CreateDatabase();
的DbCommand CMD = db.GetStoredProcCommand(Get_Some_Data);
DataSet的DS = db.ExecuteDataSet(CMD); 的foreach(在ds.Tables数据表TBL){
的foreach(在tbl.Columns的DataColumn COL){
如果(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)){
//此列是数字
}其他{
//此列不是数值
}
}
}
有是检查,如果该类型是除了它比较实际类型的数值没有什么好办法。结果
这是尤其如此,如果的的数字定义的是一个有点不同(在你的情况下,根据code, - 无符号整数不是数字)。
另一件事是,<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.datacolumn.datatype.aspx\">DataColumn.DataType根据MSDN 仅支持以下几种类型:
- 布尔
- 字节
- 字符
- 日期时间
- 小数
- 双击
- 的Int16
- 的Int32
- 的Int64
- 为SByte
- 单
- 字符串
- 时间跨度
- UINT16
- UInt32的
- UINT64
- 字节[]
在粗体类型Numerics的(我把它定义),所以你需要确保你检查。
我个人写的DataColumn的类型的扩展方法(不适用于TYPE!)。结果
我讨厌的如果... then..else 的事情,所以我代替使用的设置的基于方法,就像这样:
公共静态布尔则IsNumeric(这个山坳的DataColumn){
如果(COL == NULL)
返回false;
//使这个常量
VAR numericTypes =新[] {typeof运算(字节)的typeof(十进制)的typeof(双人间)
typeof运算(Int16的)的typeof(Int32)已的typeof(Int64的)的typeof(为SByte)
typeof运算(单人间)的typeof(UINT16)的typeof(UInt32的)的typeof(UINT64)};
返回numericTypes.Contains(col.DataType);
}
和使用将是:
如果(col.IsNumeric())......
这对我来说很容易
Is there a better way than this to check if a DataColumn in a DataTable is numeric (coming from a SQL Server database)?
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
}
}
}
There is no good way to check if the type is numeric except comparing it to the actual types.
This is especially true if the definition of numeric is a bit different (in your case, according to code, - unsigned integers are not numerics).
Another thing is that DataColumn.DataType according to MSDN only supports following types:
- Boolean
- Byte
- Char
- DateTime
- Decimal
- Double
- Int16
- Int32
- Int64
- SByte
- Single
- String
- TimeSpan
- UInt16
- UInt32
- UInt64
- Byte[]
The bolded types are numerics (as I define it) so you need to make sure you check them.
I personally would write an extension method for the DataColumn type (not for the TYPE!).
I hate the if...then..else thing so instead I use a SETS-based approach, like this:
public static bool IsNumeric(this DataColumn col) {
if (col == null)
return false;
// Make this const
var numericTypes = new [] { typeof(Byte), typeof(Decimal), typeof(Double),
typeof(Int16), typeof(Int32), typeof(Int64), typeof(SByte),
typeof(Single), typeof(UInt16), typeof(UInt32), typeof(UInt64)};
return numericTypes.Contains(col.DataType);
}
And the usage would be:
if (col.IsNumeric()) ....
which is easy enough for me
这篇关于确定是否是DataColumn的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!