有沒有更好的方法來檢查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