我有一个Access数据库中的表,我试图使用C#获取列名和每列的字符串表示的最大长度。也就是说,如果表是这样的:获取DataTable列字符表示的最大长度
Name ID SysBP
-------------------
Jerry 1234 108.1
Tim 123 140.6
Marge 6 99.0
凡ID和SysBP列是数字的专栏中,我希望有一个DataTable
对象,它包含以下信息:
ColumnName MaxCharLen
----------------------
Name 5
ID 4
SysBP 4
我有一个OLEDB连接到数据库和两个DataTable
对象,一个用于表模式,另一个用于实际表。
public DataTable GetMetadata(string tableName)
{
// At this point the _oleConnection object exists and is open...
OleDbCommand selectTable = new OleDbCommand("SELECT * FROM [" + tableName + "]",
_oleConnection);
OleDbDataReader oleReader = selectTable.ExecuteReader();
// Column names from table schema
DataTable schemaTable = oleReader.GetSchemaTable();
schemaTables.Columns.Add("MaxCharLen", typeof(int));
// Import full Access table as DataTable
DataTable tableRecords = new DataTable();
tableRecords.Load(oleReader);
// Get maximum length of string representations by column
// Populate MaxCharLen with that information
...???
}
任何人都可以提供有关如何计算该字段的任何见解吗?
的信息像'sys.columns'可能在这种情况下提供的唯一有用的部分是列名,这在我的情况下,直接从'oleReader.GetSchemaTable来()'。 –
是的,在这种情况下是真的。如果你打算在服务器上完成所有这些工作,你可以使用sys.columns来创建动态查询,而不是使用datareader。 – Brad