我使用 Northwind 示例数据库和 ODBC。对于我的 C# 应用程序,我想将列的数据类型作为 Type 对象(而不是 String),因为出于某些原因我想使用 Type。
示例(取自 Northwind) - 表 OrderDetails 包含列:
OrderID type int(11), ProductID type int(11), UnitPrice type float, etc.
我想得到类似的东西:
Type dataType = (the data type of OrderID)
不是: string dataType = reader.GetString(dataTypeOrdinal); // I can get this without any problem.
这是代码片段:(我还在代码中放了一些详细的注释)
var connectionString = Database.ConnectionString;
var command = "SELECT COLUMN_NAME, TABLE_NAME, DATA_TYPE, IS_NULLABLE from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = \'" + _name + "\'";
IDataReader reader = null;
reader = _database.Provider.ExecuteReader(command, false);
while (reader.Read())
{
var columnNameOrdinal = reader.GetOrdinal("column_name");
var dataTypeOrdinal = reader.GetOrdinal("data_type");
var columnName = reader.GetString(columnNameOrdinal);
// till line above everything works fine
// ---------my problem starts below ---------------
// I don't need data type as string like this:
// var dataTypeName = reader.GetString(dataTypeOrdinal);
// but rather something like: Type dataType = (the data type of column)
// I tried using reader.GetData(dataTypeOrdinal), but I get
// System.NotSupportedException: "Specified method is not supported."
}
非常感谢任何帮助和提示。提前谢谢。