2011-01-19 58 views
0

看:这段代码获取表的架构

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中显示。

回答

0

尝试以下操作:

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