private void LoadSchemaFile2(string fileName) { if (!File.Exists(fileName)) { return; } TableSchema tableSchema = new TableSchema(); tableSchema.ReadXml(fileName); foreach (TableSchema.TableNamesRow table in tableSchema.TableNames.Rows) { TableSchema.TableNamesRow row = _tableSchema.TableNames.FindByTableName(table.TableName); if (row != null) { row.ItemName = table.ItemName; row.CollectionName = table.CollectionName; row.Include = table.Include; row.IsDBReadOnly = table.IsDBReadOnly; } } foreach (TableSchema.FieldsRow field in tableSchema.Fields.Rows) { TableSchema.FieldsRow row = _tableSchema.Fields.FindByTableNameFieldName(field.TableName, field.FieldName); if (row != null) { row.CustomType = field.CustomType; row.IsReadOnly = field.IsReadOnly; row.AllowDBNull = field.AllowDBNull; row.IsPrimaryKey = field.IsPrimaryKey; } } AppSettings.Default.LastSchema = fileName; SetSchemaFileName(fileName); SetSchemaModified(false); }
private bool LoadDBSchema() { try { SetSchemaFileName(""); SetSchemaModified(false); _tableSchema.Clear(); using (SqlConnection connection = new SqlConnection(GetConnectionString(_databaseName))) { connection.Open(); //Forms.Form1.DoIt(connection); DataTable table = connection.GetSchema("Tables"); foreach (DataRow row in table.Rows) { string tableName = (string)row["TABLE_NAME"]; TableSchema.TableNamesRow tableNameRow = _tableSchema.TableNames.NewTableNamesRow(); tableNameRow.TableName = tableName; tableNameRow.Include = false; string collection = tableName; string item = tableName; if (tableName.Substring(tableName.Length - 3, 3) == "ies") { item = tableName.Remove(tableName.Length - 3, 3) + "y"; } else if (tableName.Substring(tableName.Length - 2, 2) == "es") { //collection = tableName + "Collection"; item = tableName.Remove(tableName.Length - 2, 2); } else if (tableName.Substring(tableName.Length - 1, 1) == "s") { item = tableName.Remove(tableName.Length - 1, 1); } else { item = item + "Item"; } tableNameRow.ItemName = item; tableNameRow.CollectionName = tableName; tableNameRow.BaseCollectionClass = AppSettings.Default.BaseCollectionClass; tableNameRow.BaseItemClass = AppSettings.Default.BaseItemClass; tableNameRow.IsDBReadOnly = false; _tableSchema.TableNames.AddTableNamesRow(tableNameRow); LoadTableSchema(tableName, connection); } tableNamesBindingSource.DataSource = _tableSchema; tableNamesBindingSource.DataMember = "TableNames"; tableNamesBindingSource.Sort = "TableName"; fieldsBindingSource.DataSource = _tableSchema; fieldsBindingSource.DataMember = "Fields"; gridFields.AutoGenerateColumns = true; gridFields.Columns[0].Visible = false; gridTables.AutoGenerateColumns = true; connection.Close(); gridFields.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); foreach (DataGridViewColumn column in gridFields.Columns) { if (column.ReadOnly) { column.DefaultCellStyle.BackColor = SystemColors.GradientInactiveCaption; } } } SetSchemaModified(false); } catch (Exception ex) { MessageBox.Show("Unable to retrieve the database schema.\n\n" + ex.Message); return(false); } return(true); }