1

I want to gather information about an specific table, So this is my Code:

StringBuilder sb = new StringBuilder();
sb.AppendLine();
ServerConnection conn = new ServerConnection();
conn.ConnectionString = "";//MyConnection string
Server srv = new Server(conn);
Database db = srv.Databases["MyDataBase"];
Table tb = db.Tables["MyTable"];
foreach(Column c in tb.Columns) {

   sb.AppendLine(c.Name + " -> " + c.DataType.ToString());
   sb.AppendLine(c.ExtendedProperties["MS_Description"].Value.ToString());

  if(c.IsForeignKey){

    //Need to get reference here:
    //Sb.AppendLine("ReferenceColumn")
    //Sb.AppendLine("ReferenceTable")
    //Sb.AppendLine("ReferenceSchema")
  }
}

I find this one but this link get Foreignkeys at first, is there any way to find reference here (mean by Column type)? What is your suggestion in this situation?

Community
  • 1
  • 1

1 Answers1

1

Not sure whether this is what you are looking for!

foreach (ForeignKey FKey in currentTable.ForeignKeys)
{
    foreach (ForeignKeyColumn FKColumn in FKey.Columns)
    {
        //Hope this is what you are looking for!
        //Check the output of FKColumn.Name
        //Check the output of FKey.ReferencedTable
    }
}
vmvadivel
  • 1,041
  • 5
  • 7