开发者

use GetOleDbSchemaTable to get the columns of a table named "Street"

开发者 https://www.devze.com 2023-01-20 00:35 出处:网络
I\'m trying to read the column names of a table \"Streets\" in an Access database by opening an OleDbConnection.I call GetOleDbSchemaTable but I can\'t seem to figure out h开发者_运维问答ow to get at

I'm trying to read the column names of a table "Streets" in an Access database by opening an OleDbConnection. I call GetOleDbSchemaTable but I can't seem to figure out h开发者_运维问答ow to get at my columns.

I'd like to use .NET 3.5 framework if possible.


using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    connection.Open();

    DataTable tableColumns = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, "Streets", null }));
    foreach (DataRow row in tableColumns.Rows)
    {
        var columnNameColumn = row["COLUMN_NAME"];
        var dateTypeColumn = row["DATA_TYPE"];
        var ordinalPositionColumn = row["ORDINAL_POSITION"];
        ...
    }
}


This works for me, puts column names of table street in a list

OleDbCommand mdbCommand = new OleDbCommand("SELECT * FROM Streets", mdbConnection);
OleDbDataReader myReader = mdbCommand.ExecuteReader(CommandBehavior.KeyInfo);
DataTable  schemaTable = myReader.GetSchemaTable();
List<String> columnNameList = new List<string>();
foreach (DataRow myField in schemaTable.Rows)
{
foreach (DataColumn myProperty in schemaTable.Columns)
{
if (myProperty.ColumnName == "ColumnName") columnNameList.Add(myField[myProperty].ToString());
}

}
0

精彩评论

暂无评论...
验证码 换一张
取 消