C# 确定 DataColumn 是否为数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1725903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if DataColumn is numeric
提问by JustinStolle
Is there a better way than this to check if a DataColumn in a DataTable is numeric (coming from a SQL Server database)?
有没有比这更好的方法来检查 DataTable 中的 DataColumn 是否是数字(来自 SQL Server 数据库)?
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
}
}
}
采纳答案by Dmytrii Nagirniak
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 numericis a bit different (in your case, according to code, - unsigned integers are not numerics).
除了将其与实际类型进行比较之外,没有什么好方法可以检查类型是否为数字。
如果数字的定义有点不同(在您的情况下,根据代码,无符号整数不是数字),则尤其如此。
Another thing is that DataColumn.DataType according to MSDNonly supports following types:
另一件事是DataColumn.DataType 根据 MSDN只支持以下类型:
- Boolean
- Byte
- Char
- DateTime
- Decimal
- Double
- Int16
- Int32
- Int64
- SByte
- Single
- String
- TimeSpan
- UInt16
- UInt32
- UInt64
- Byte[]
- 布尔值
- 字节
- 字符
- 约会时间
- 十进制
- 双倍的
- 整数16
- 整数32
- 64位
- 字节
- 单身的
- 细绳
- 时间跨度
- UInt16
- UInt32
- UInt64
- 字节[]
The boldedtypes are numerics (as I define it) so you need to make sure you check them.
该加粗类型NUMERICS(我定义它),所以你需要确保你检查。
I personally would write an extension method for the DataColumn type (not for the TYPE!).
I hate the if...then..elsething so instead I use a SETS-based approach, like this:
我个人会为 DataColumn 类型(不是为 TYPE!)编写扩展方法。
我讨厌if...then...else事情,所以我使用基于SETS的方法,如下所示:
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
这对我来说很容易
回答by Jonathan
Maybe you could make it shorter with:
也许你可以用以下方法缩短它:
System.Type theType = col.DataType AS System.Type
if(theType == System.Single || theType == System.Double...) {}
回答by S.Serpooshan
Another method without using Arrays, just by one line of code:
另一种不使用数组的方法,只需一行代码:
return col != null && "Byte,Decimal,Double,Int16,Int32,Int64,SByte,Single,UInt16,UInt32,UInt64,".Contains(col.DataType.Name + ",");
This line of code can be used either as a normal helper method or as an extension method.
这行代码既可以用作普通的辅助方法,也可以用作扩展方法。