internal void CollectFields(IDbConnection p_Conn) { string commandText = "SELECT c.Column_Name, c.Column_Default, c.Is_Nullable, c.Data_Type, c.Character_Maximum_Length, c.Numeric_Precision, c.Numeric_Scale FROM Information_Schema.Columns c WHERE Table_Name = @Table ORDER BY Ordinal_Position ; "; List <DBFieldInfo> list = new List <DBFieldInfo>(); IDbCommand dbCommand = null; IDataReader dataReader = null; try { dbCommand = p_Conn.CreateCommand(); dbCommand.CommandType = CommandType.Text; dbCommand.CommandText = commandText; IDbDataParameter dbDataParameter = dbCommand.CreateParameter(); dbDataParameter.ParameterName = "Table"; dbDataParameter.Value = this._TableName; dbCommand.Parameters.Add(dbDataParameter); dataReader = dbCommand.ExecuteReader(); while (dataReader.Read()) { int num = 0; string p_Column_Name = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; string p_Column_Default = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; string text = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; string p_Data_Type = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; int p_Character_Maximum_Length = dataReader.IsDBNull(num) ? 0 : dataReader.GetInt32(num); num++; int p_Numeric_Precision = (int)(dataReader.IsDBNull(num) ? 0 : dataReader.GetByte(num)); num++; int p_Numeric_Scale = dataReader.IsDBNull(num) ? 0 : dataReader.GetInt32(num); num++; bool p_Is_Nullable = text.Equals("YES"); DBFieldInfo item = new DBFieldInfo(p_Column_Name, p_Column_Default, p_Is_Nullable, p_Character_Maximum_Length, p_Numeric_Precision, p_Numeric_Scale, p_Data_Type); list.Add(item); } } finally { if (dataReader != null) { dataReader.Close(); } if (dbCommand != null) { dbCommand.Dispose(); } } this._DbFieldInfo = list.ToArray(); this.CollectPrimaryKeys(p_Conn); this.CollectForeignKeys(p_Conn); this.CollectDependentTables(p_Conn); }
private void CollectPrimaryKeys(IDbConnection p_Conn) { string commandText = "SELECT tc.CONSTRAINT_NAME, tc.TABLE_NAME, ccu.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON\tccu.CONSTRAINT_CATALOG = tc.CONSTRAINT_CATALOG\tAND ccu.TABLE_NAME = tc.TABLE_NAME\tAND ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME WHERE tc.CONSTRAINT_TYPE = 'PRIMARY KEY' AND tc.TABLE_NAME = @Table"; IDbCommand dbCommand = null; IDataReader dataReader = null; try { dbCommand = p_Conn.CreateCommand(); dbCommand.CommandType = CommandType.Text; dbCommand.CommandText = commandText; IDbDataParameter dbDataParameter = dbCommand.CreateParameter(); dbDataParameter.ParameterName = "Table"; dbDataParameter.Value = this._TableName; dbCommand.Parameters.Add(dbDataParameter); dataReader = dbCommand.ExecuteReader(); while (dataReader.Read()) { int num = 0; string arg_7A_0 = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; string arg_97_0 = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; string value = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; DBFieldInfo[] dbFieldInfo = this._DbFieldInfo; for (int i = 0; i < dbFieldInfo.Length; i++) { DBFieldInfo dbFieldInfo2 = dbFieldInfo[i]; if (dbFieldInfo2.Column_Name.Equals(value)) { dbFieldInfo2.Is_Primary_Key = true; } } } } finally { if (dataReader != null) { dataReader.Close(); } if (dbCommand != null) { dbCommand.Dispose(); } dataReader = null; dbCommand = null; } }
public DBFieldInfo GetPkField() { DBFieldInfo result = null; DBFieldInfo[] dbFieldInfo = this._DbFieldInfo; for (int i = 0; i < dbFieldInfo.Length; i++) { DBFieldInfo dbFieldInfo2 = dbFieldInfo[i]; if (dbFieldInfo2.Is_Primary_Key) { result = dbFieldInfo2; break; } } return(result); }
public bool HasProperty(string prop) { DBFieldInfo[] dbFieldInfo = this._DbFieldInfo; bool result; for (int i = 0; i < dbFieldInfo.Length; i++) { DBFieldInfo dbFieldInfo2 = dbFieldInfo[i]; if (dbFieldInfo2.Column_Name == prop) { result = true; return(result); } } result = false; return(result); }
public string GetLabel() { return(DBFieldInfo.MakeLabel(this._TableName)); }
private void CollectForeignKeys(IDbConnection p_Conn) { string commandText = "SELECT FK.TABLE_NAME AS K_Table, CU.COLUMN_NAME AS FK_Column, PK.TABLE_NAME AS PK_Table, PT.COLUMN_NAME AS PK_Column, C.CONSTRAINT_NAME AS Constraint_Name FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU\tON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME INNER JOIN (\t\tSELECT i1.TABLE_NAME, i2.COLUMN_NAME\t\tFROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1\t\tINNER JOIN\tINFORMATION_SCHEMA.KEY_COLUMN_USAGE i2\t\t\tON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME\t\tWHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'\t\t) PT ON PT.TABLE_NAME = PK.TABLE_NAME WHERE FK.TABLE_NAME = @Table"; List <DBForeignKeyInfo> list = new List <DBForeignKeyInfo>(); IDbCommand dbCommand = null; IDataReader dataReader = null; try { dbCommand = p_Conn.CreateCommand(); dbCommand.CommandType = CommandType.Text; dbCommand.CommandText = commandText; IDbDataParameter dbDataParameter = dbCommand.CreateParameter(); dbDataParameter.ParameterName = "Table"; dbDataParameter.Value = this._TableName; dbCommand.Parameters.Add(dbDataParameter); dataReader = dbCommand.ExecuteReader(); while (dataReader.Read()) { int num = 0; string arg_8D_0 = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; string p_FK_Column = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; string p_PK_Table = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; string p_PK_Column = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; string p_Constraint_Name = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; DBForeignKeyInfo item = new DBForeignKeyInfo(p_FK_Column, p_PK_Table, p_PK_Column, p_Constraint_Name, "Name"); list.Add(item); } } finally { if (dataReader != null) { dataReader.Close(); } if (dbCommand != null) { dbCommand.Dispose(); } dataReader = null; dbCommand = null; } commandText = "SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE DATA_TYPE IN ('nvarchar', 'varchar') AND CHARACTER_MAXIMUM_LENGTH > 3 AND TABLE_NAME = @Table ORDER BY CASE WHEN COLUMN_NAME LIKE '%NAME%' THEN 0 ELSE 1 END, ORDINAL_POSITION"; foreach (DBForeignKeyInfo current in list) { try { dbCommand = p_Conn.CreateCommand(); dbCommand.CommandType = CommandType.Text; dbCommand.CommandText = commandText; IDbDataParameter dbDataParameter = dbCommand.CreateParameter(); dbDataParameter.ParameterName = "Table"; dbDataParameter.Value = current.PK_Table; dbCommand.Parameters.Add(dbDataParameter); dataReader = dbCommand.ExecuteReader(); while (dataReader.Read()) { int num = 0; string pK_DescriptorColumn = dataReader.IsDBNull(num) ? "" : dataReader.GetString(num); num++; current.PK_DescriptorColumn = pK_DescriptorColumn; } } finally { if (dataReader != null) { dataReader.Close(); } if (dbCommand != null) { dbCommand.Dispose(); } dataReader = null; dbCommand = null; } DBFieldInfo[] dbFieldInfo = this._DbFieldInfo; for (int i = 0; i < dbFieldInfo.Length; i++) { DBFieldInfo dbFieldInfo2 = dbFieldInfo[i]; if (dbFieldInfo2.Column_Name.Equals(current.FK_Column)) { dbFieldInfo2.ForeignKeyInfo = current; break; } } } }
public string GetLabel() { return(DBFieldInfo.MakeLabel(this._Column_Name)); }