internal class AccessSchema
{
private Field GetAccessField(string connectionString, string tbName, DataRow r)
{
Field field = new Field();
field.AllowNull = ObjectHelper.GetBool(r["IS_NULLABLE"]);
field.DefaultValue = ObjectHelper.GetString(r["COLUMN_DEFAULT"]);
field.FieldDescn = ObjectHelper.GetString(r["DESCRIPTION"]);
field.FieldLength = ObjectHelper.GetInt(r["CHARACTER_MAXIMUM_LENGTH"]);
field.FieldName = ObjectHelper.GetString(r["COLUMN_NAME"]);
field.FieldNumber = ObjectHelper.GetInt(r["ORDINAL_POSITION"]);
field.FieldSize = ObjectHelper.GetInt(r["CHARACTER_OCTET_LENGTH"]);
field.OleDbTypeString = ObjectHelper.GetString(r["DATA_TYPE"]);
DataTable table = this.GetDbSchema(connectionString, OleDbSchemaGuid.Primary_Keys, null);
foreach (DataRow row in table.Rows)
{
if ((row[2].ToString() == tbName) && (row[3].ToString() == field.FieldName))
{
field.IsKeyField = true;
field.IsIdentifier = true;
}
}
field.TableName = ObjectHelper.GetString(r["TABLE_NAME"]);
return field;
}
private DataTable GetDbSchema(string connString, Guid schema, object[] restrictions)
{
OleDbConnection connection = new OleDbConnection(connString);
connection.Open();
DataTable oleDbSchemaTable = connection.GetOleDbSchemaTable(schema, restrictions);
connection.Close();
return oleDbSchemaTable;
}
public bool GetSchema(Database database)
{
try
{
object[] restrictions = new object[4];
restrictions[3] = "table";
DataTable table = this.GetDbSchema(database.ConnectionString, OleDbSchemaGuid.Tables, restrictions);
foreach (DataRow row in table.Rows)
{
Table table2 = new Table();
table2.Name = row["TABLE_NAME"].ToString();
restrictions = new object[3];
restrictions[2] = table2.Name;
DataTable table3 = this.GetDbSchema(database.ConnectionString, OleDbSchemaGuid.Columns, restrictions);
foreach (DataRow row2 in table3.Rows)
{
table2.Fields.Add(this.GetAccessField(database.ConnectionString, table2.Name, row2));
}
database.Tables.Add(table2);
}
DataTable table4 = this.GetDbSchema(database.ConnectionString, OleDbSchemaGuid.Views, null);
foreach (DataRow row3 in table4.Rows)
{
database.Views.Add(row3["TABLE_NAME"].ToString());
}
DataTable table5 = this.GetDbSchema(database.ConnectionString, OleDbSchemaGuid.Procedures, null);
foreach (DataRow row4 in table5.Rows)
{
database.StoreProcedures.Add(row4["PROCEDURE_NAME"].ToString());
}
return true;
}
catch
{
return false;
}
}
}