数据库A,B结构完全相同。A中的表T1,B中的表T1结构完全相同。两个表之间的更新。
方法一:用sql存储进程,可以实现,这个没问题。
方法二:想用dataset方式更相互更新,怎么实现?在网上看了一堆关于dataset更新的贴子,但没有解决问题。
        SqlDataAdapter daTA = new SqlDataAdapter("select * from t1", A);
        SqlCommandBuilder cbTa = new SqlCommandBuilder(daTA);
        DataSet dsA = new DataSet();
        daTa.Fill(dsA,"t1");                   SqlDataAdapter daTB = new SqlDataAdapter("select * from t1", B);
        SqlCommandBuilder cbTB = new SqlCommandBuilder(daTB);
        DataSet dsA = new DataSet();
        daTB.Fill(dsB,"t1");                   以下的代码怎么写,请大家帮忙?-------------------------------------
另:方法一和方法二哪种效率更高?

解决方案 »

  1.   

    使用 SqlBulkCopy 类 ,但是需要ado.net 2.0
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  2.   

                dsTB = dsTA.Copy();//dsTA.Merge(dsTB);
                int iRows= dsTB.Tables[0].Rows.Count;//数据也已经传到了dsTB里
                daTB.Update(dsTB,"t1");
                dsTB.AcceptChanges();
              //但daTB中并没有数据,B库中的表t1也无数据。
      

  3.   

    1楼的高手能不能把怎么用SqlBulkCopy写个例子出来?
      

  4.   

    http://msdn2.microsoft.com/zh-cn/library/8x2hdfta(VS.80).aspxusing System.Data.SqlClient;class Program
    {
        static void Main()
        {
            string connectionString = GetConnectionString();
            // Open a sourceConnection to the AdventureWorks database.
            using (SqlConnection sourceConnection =
                       new SqlConnection(connectionString))
            {
                sourceConnection.Open();            // Perform an initial count on the destination table.
                SqlCommand commandRowCount = new SqlCommand(
                    "SELECT COUNT(*) FROM " +
                    "dbo.BulkCopyDemoMatchingColumns;",
                    sourceConnection);
                long countStart = System.Convert.ToInt32(
                    commandRowCount.ExecuteScalar());
                Console.WriteLine("Starting row count = {0}", countStart);            // Get data from the source table as a SqlDataReader.
                SqlCommand commandSourceData = new SqlCommand(
                    "SELECT ProductID, Name, " +
                    "ProductNumber " +
                    "FROM Production.Product;", sourceConnection);
                SqlDataReader reader =
                    commandSourceData.ExecuteReader();            // Set up the bulk copy object using a connection string. 
                // In the real world you would not use SqlBulkCopy to move
                // data from one table to the other in the same database.
                using (SqlBulkCopy bulkCopy =
                           new SqlBulkCopy(connectionString))
                {
                    bulkCopy.DestinationTableName =
                        "dbo.BulkCopyDemoMatchingColumns";                try
                    {
                        // Write from the source to the destination.
                        bulkCopy.WriteToServer(reader);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        // Close the SqlDataReader. The SqlBulkCopy
                        // object is automatically closed at the end
                        // of the using block.
                        reader.Close();
                    }
                }            // Perform a final count on the destination 
                // table to see how many rows were added.
                long countEnd = System.Convert.ToInt32(
                    commandRowCount.ExecuteScalar());
                Console.WriteLine("Ending row count = {0}", countEnd);
                Console.WriteLine("{0} rows were added.", countEnd - countStart);
                Console.WriteLine("Press Enter to finish.");
                Console.ReadLine();
            }
        }    private static string GetConnectionString()
            // To avoid storing the sourceConnection string in your code, 
            // you can retrieve it from a configuration file. 
        {
            return "Data Source=(local); " +
                " Integrated Security=true;" +
                "Initial Catalog=AdventureWorks;";
        }
    }
      

  5.   

    方法一效率更高方法二你要把数据从SQL Server读到你的程序运行的机器内存里,然后再写回SQL Server.如果你运行程序的机器和SQL Server不在同一机器,我想这个效率差别就更明显了.其实如果没有特别的需求的话一句SQL就可以搞定了:INSERT INTO t2
    (
      c1,
      c2,
      c3
    )
    SELECT c1,c2,c3 FROM t1
      

  6.   

    Sorry,没看到楼主写的2个数据库...如果你的2个数据库不在同一个DB Server上的话,那么需要加上link server才能直接用SQL完成数据copy动作.如果2个DB Server间没有设置link server,那么就只能用SqlBulkCopy的方式.
      

  7.   

    呵呵,数据库A在本地,数据库B为远程数据库。
    我明白效率肯定是用进程写高过用dataset。
    但希望能搞明白用dataset怎么成功实现呢?
      

  8.   

    用存储过程效率高些.
    用dataSet的话,你需要比较这两个表的数据,如果存在就要比较是不是一样,如果一样就什么都不做,如果不一样,就把不一样的行的状态改为跟新,并把数据修改,如果不存在就生成一个新行加进去,这样你调用Update方法才可以更新数据库里的数据,它是根据DataSet的表里的行的状态执行相应操作的。你这样做他什么操作都不做,数据不变的。
    用DataSet太麻烦了。
      

  9.   

    呵呵,只是希望搞明白dataset怎么实现呢?用成功的代码吗?谢谢。
      

  10.   

    先结贴吧,还是自己慢慢摸索,先用存储进程做完工作,然后再试着用dataset看看能不能成功。