有没有更好的方法来检查DataTable中的DataColumn是否是数值(来自SQL Server数据库)?确定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
}
}
}
+1用于扩展方法,让痛苦保持1位 – 2009-11-12 23:10:14
我没有包含无符号整数类型,因为它们没有在http://msdn.microsoft.com/en-us/library/ms131092%28SQL中列出.90%29.aspx但我喜欢你的方法。 – JustinStolle 2009-11-12 23:11:07
@JustinStolle,根据我提供的MSDN页面,我最好包含未签名的类型。您引用的页面是特定于SQL Server 2005的页面。 – 2009-11-12 23:14:07