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;
}
精彩评论