2

2 つのデータベース間で 2 つのテーブルを同期しようとしています。これを行う最善の方法は、DataTable.Merge メソッドを使用することだと思いました。これは変更を反映しているように見えますが、データベースには何もコミットされません。

これまでのところ、私は持っています:

string tableName = "[Table_1]";
        string sql = "select * from " + tableName;

        using (SqlConnection sourceConn = new SqlConnection(ConfigurationManager.ConnectionStrings["source"].ConnectionString))
        {
            SqlDataAdapter sourceAdapter = new SqlDataAdapter();

            sourceAdapter.SelectCommand = new SqlCommand(sql, sourceConn);
            sourceConn.Open();
            DataSet sourceDs = new DataSet();
            sourceAdapter.Fill(sourceDs);

            using (SqlConnection targetConn = new SqlConnection(ConfigurationManager.ConnectionStrings["target"].ConnectionString))
            {
                SqlDataAdapter targetAdapter = new SqlDataAdapter();

                targetAdapter.SelectCommand = new SqlCommand(sql, targetConn);

                SqlCommandBuilder builder = new SqlCommandBuilder(targetAdapter);

                targetAdapter.InsertCommand = builder.GetInsertCommand();
                targetAdapter.UpdateCommand = builder.GetUpdateCommand();
                targetAdapter.DeleteCommand = builder.GetDeleteCommand();

                targetConn.Open();
                DataSet targetDs = new DataSet();
                targetAdapter.Fill(targetDs);

                targetDs.Tables[0].TableName = tableName;
                sourceDs.Tables[0].TableName = tableName;
                targetDs.Tables[0].Merge(sourceDs.Tables[0]);

                targetAdapter.Update(targetDs.Tables[0]);
            }

        }

現在、ソースにはターゲットにない行が 1 つあります。この行は転送されません。空のターゲットでも試しましたが、何も転送されません。

4

2 に答える 2

0

あなたの質問に直接答えているわけではないことはわかっていますが、Microsoft Sync Frameworkを見ましたか?

この種のことを行うように設計されており、ロバの作業のほとんど (すべてではないにしても) を行います。

于 2010-04-19T13:40:43.890 に答える
0

これはやり過ぎかもしれませんが、Microsoft の Sync Framework を試すことができます。

于 2010-04-19T13:41:24.940 に答える