I would like to retrieve a list of the columns for all the tables in the list matchingTableList:
foreach (var table in matchingTableList)
{
Console.WriteLine("Analyzing " + table);
var sourceTableRows = sqlSourceConnection.GetSchema("Columns", new[] {SourceDatabase, null, table, null});
var destTableRows = sqlDestConnection.GetSchema("Columns", new[] { DestDatabase, null, table, null });
Console.WriteLine("Number of fields " + sourceTableRows.Rows.Count);
//iterate through every field in the table
foreach (var row in sourceTableRows.Rows)
{
Console.WriteLine("Analyzing col " + row);
//if the field is not present, add it to the list containing the added fields for the current table
if (destTableRows.Columns.Contains((row.ToString()))) continue;
if (!changedTablesAddedFields[table].Contains(row.ToString()))
{
changedTablesAddedFields[table].Add(row.ToString());
}
}
}
The count of the sourceTableRows.Rows is always 0, even if the tables have columns in the database. What am I missing?
EDIT1: I can retrieve the values when I do
var sourceTableRows = sqlSourceConnection.GetSchema("Columns", new[] {null, null, table, null});
var destTableRows = sqlDestConnection.GetSchema("Columns", new[] { null, null, table, null });
But why does it work like that?
EDIT2: I found out that this is the syntax for MSSQL:
var sourceTableRows = sqlSourceConnection.GetSchema("Columns", new[] {SourceDatabase, null, table, null});
var destTableRows = sqlDestConnection.GetSchema("Columns", new[] { DestDatabase, null, table, null });
While MySQL requires this:
var sourceTableRows = sqlSourceConnection.GetSchema("Columns", new[] { null, SourceDatabase, table, null});
var destTableRows = sqlDestConnection.GetSchema("Columns", new[] { null, DestDatabase, table, null });
Still, when I try to get the name of the column with
var rowName = (string) row[2];
The only thing I get is the name of the table. I debugged the application and I cannot find the columns name in the row variable. What am I missing?
1条答案
按热度按时间enyaitl31#
Try the following code snippet (used for SqlCE but applicable to other versions of SQL Server: just use the corresponding SQL objects). Also, pay attention to the comments as they pretty much explain the code operational logic:
You can also use a short-cut to shorten the code:
Hope this will help. Rgds,
PS. In your own code, as a debugging recommendation: put a break line and make sure that
sourceDataTable !=null