获取表的架构

时间:2011-01-19 12:29:05

标签: c# asp.net

看看这段代码:

public void GetScheme4Table(string tableName)
{
    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\" + DdListDatabases4GettingTables.SelectedValue + ".mdb;Persist Security Info=True");
    conn.Open();
    DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
                                                     new object[] { null, null, null, tableName });
    GridViewTablesScheme.DataSource = schemaTable;
    GridViewTablesScheme.DataBind();
    conn.Close();  
}

架构数据表中没有行如何解决这个问题?

我想获取表格的架构并在gridview中显示。

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

public DataTable GetScheme4Table(string tableName)
{
    DataTable ret = null;
    IDbCommand command = null;

    using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\" + DdListDatabases4GettingTables.SelectedValue + ".mdb;Persist Security Info=True"))
    {
        command = connection.CreateCommand();
        command.CommandText = string.Format("SELECT TOP 1 * FROM [{0}]", tableName);
        using (IDataReader reader = command.ExecuteReader(CommandBehavior.KeyInfo))
        {
            ret = reader.GetSchemaTable();
        }
    }

    return ret;
}