3

SMO を使用して 2 つのリモート サーバー間でテーブルを転送するための C# コードを作成しました。知りたいのは、正確なスキーマ、列名、データ型、制約、およびすべての。そうすれば、毎回既存のテーブルを削除して新しいテーブルを作成する必要がなくなります。

4

1 に答える 1

2

次のコード スニペットを試してください。

Server srv1 = new Server("<server_location>");
srv1.ConnectionContext.LoginSecure = false;
srv1.ConnectionContext.Login = "<username>";
srv1.ConnectionContext.Password = "<password>";
srv1.ConnectionContext.Connect();
Database sourceDb = srv1.Databases["<database_name>"];
Table sourceTbl = sourceDb.Tables["<table_name>"];

Server srv2 = new Server("<server_location>");
srv2.ConnectionContext.LoginSecure = false;
srv2.ConnectionContext.Login = "<username>";
srv2.ConnectionContext.Password = "<password>";
srv2.ConnectionContext.Connect();
Database destinationDb = srv1.Databases["<database name>"];
Table destinationTbl = sourceDb.Tables["<table_name>"];

var isMatched = CompareTables(sourceTbl, destinationTbl);    

比較方法:

bool CompareTables(Table source, Table destination)
{
    // Column count doesn't match
    if (!source.Columns.Count.Equals(destination.Columns.Count))
        return false;

    // Assuming the order of the Columns are same in both the Tables
    for (int i = 0; i < source.Columns.Count; i++)
        if (!source.Columns[i].Equals(destination.Columns[i]))
            return false;

    // Constraints count doesn't match
    if (!source.Checks.Count.Equals(destination.Checks.Count))
        return false;

    // Assuming the order of the Contraints are same in both the Tables
    for (int i = 0; i < source.Checks.Count; i++)
        if (!source.Checks[i].Equals(destination.Checks[i]))
            return false;

    return true;
}
于 2012-10-30T06:52:26.703 に答える