开发者

getting schema for a table

开发者 https://www.devze.com 2023-02-05 17:42 出处:网络
look at this code : public void GetScheme4Table(string tableName) { OleDbConnection conn = new OleDbConnection(\"Provider=Microsoft.Jet.OLEDB.4.0;Data Source开发者_开发问答=|DataDirectory|\\\\\" + D

look at this code :

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();  
}

There is no rows in the schema data table how I can solve this ?

I want to get schema for my table and show in the gridview.


Try the following:

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;
}
0

精彩评论

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