private void LoadTableSchema(string tableName, SqlConnection connection) { DataSet dataset = new DataSet(); string sql = "SELECT * FROM " + tableName + " WHERE 0=1"; SqlDataAdapter adapter = new SqlDataAdapter(sql, connection); adapter.FillSchema(dataset, SchemaType.Source); SqlCommand command = new SqlCommand(sql, connection); SqlDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly); DataTable table = reader.GetSchemaTable(); reader.Close(); foreach (DataRow row in table.Rows) { TableSchema.FieldsRow field = _tableSchema.Fields.NewFieldsRow(); field.TableName = tableName; field.FieldName = (string)row["ColumnName"]; field.DBType = (string)row["DataTypeName"]; SqlDbType sqlDbType = (SqlDbType)row["ProviderType"]; field.SqlDbType = "SqlDbType." + (sqlDbType).ToString(); field.SystemType = row["DataType"].ToString(); // (string)row["DataType"]; field.CustomType = ObjectGenerator.ConvertSystemTypeToShortHand(field.SystemType); int size = (int)row["ColumnSize"]; field.Size = (sqlDbType == SqlDbType.VarChar || sqlDbType == SqlDbType.NVarChar) && size > 8000 ? -1 : size; field.Precision = (short)row["NumericPrecision"]; field.Scale = (short)row["NumericScale"]; field.IsIdentity = (bool)row["IsIdentity"]; field.IsAutoInc = (bool)row["IsAutoIncrement"]; field.IsReadOnly = (bool)row["IsReadOnly"]; field.AllowDBNull = (bool)row["AllowDBNull"]; field.IsNullable = field.AllowDBNull && field.CustomType != "string"; field.IsPrimaryKey = row.Table.Columns.Contains("IsPrimaryKey") ? (bool)row["IsPrimaryKey"] : false; for (int i = 0; i < dataset.Tables[0].PrimaryKey.Length; i++) { if (field.FieldName == dataset.Tables[0].PrimaryKey[i].ColumnName) { field.IsPrimaryKey = true; } } _tableSchema.Fields.AddFieldsRow(field); } }
private void LoadField(Field field, TableSchema.FieldsRow row) { field.PropertyName = row.PropertyName; field.FieldName = row.FieldName; field.DBType = row.DBType; field.CustomType = row.CustomType; field.SystemType = row.SystemType; field.SqlDbType = row.SqlDbType; field.Size = row.Size; field.Scale = row.Scale; field.Precision = row.Precision; field.IsAutoInc = row.IsAutoInc; field.IsPrimaryKey = row.IsPrimaryKey; field.IsReadOnly = row.IsReadOnly; field.AllowDBNull = row.AllowDBNull; field.IsIdentity = row.IsIdentity; field.IsNullable = row.IsNullable; }
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); }