【发布时间】:2014-04-01 17:43:07
【问题描述】:
我在 Access 数据库中有一个表,我正在尝试使用 C# 来获取列名和每列的字符串表示的最大长度。也就是说,如果表格如下所示:
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
...???
}
谁能提供有关如何计算该字段的任何见解?
【问题讨论】: