I need to get the column names, primary keys, foreign keys, and other schema info. The DataTable
class seem to contain all of those.
Below is the current code I got so far. With it I could retrieve all info except the foreign keys. I'm expecting them to be defined in DataTable.Constraints
but they are not. This is my current code:
private static DataTable LoadSchemaInfo(string tableName, SqlConnection connection)
{
string cmdText = "SELECT * FROM [" + tableName + "] WHERE 1 = 0";
// Create a SqlDataAdapter to get the results as DataTable
var sqlDataAdapter = new SqlDataAdapter(cmdText, connection);
// Create a new DataTable
var dataTable = new DataTable(tableName);
// Fill the DataTable with the result of the SQL statement
sqlDataAdapter.FillSchema(dataTable, SchemaType.Source);
return dataTable;
}
Any idea how to retrieve all info or how to get the FK (preferably without using the pure SQL syntax because I would then lack of some compile-time checking)?