微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – bulkCopy.WriteToServer不复制

我正在尝试使用bulkcopy.WritetoServer()将数据从远程sql Server Express复制到本地sql Server Express,但是没有数据写入我的本地sql Server Express数据库.

代码立即跳过WritetoServer()方法…我不知道它是否在内部失败并且没有显示错误消息

我已阅读How to duplicate a SQL Server 2000 table programatically using .NET 2.0?,而且我使用的代码非常相似.虽然我在远程和sql Server 2014 Express本地使用sql Server 2008 Express:

using (sqlConnection remoteConnection = new sqlConnection(remoteConnectionString))
{
    var query = "SELECT * FROM @R_141_4045@ion_schema.tables WHERE table_type = 'base table'";
    sqlCommand commandGetTables = new sqlCommand(query,remoteConnection);

    try
    {
        remoteConnection.open();
        sqlDataReader results = commandGetTables.ExecuteReader();

        while (results.Read())
        {
            tables.Add(results.GetString(2));
        }

        results.Close();
    }
    catch (Exception ex)
    {
        //stuff
    }
    finally
    {
        remoteConnection.Close();
    }

    remoteConnection.open();

    foreach (var table in tables)
    {                    
        // Get data from the source table as a sqlDataReader.
        var commandSourceData = new sqlCommand("SELECT * FROM " + table + ";",remoteConnection);
        var reader = commandSourceData.ExecuteReader();

        using (sqlConnection destinationConnection = new sqlConnection(destinationConnectionString))
        {
            destinationConnection.open();

            using (var bulkcopy = new sqlBulkcopy(destinationConnection))
            {
                bulkcopy.DestinationTableName = table;

                try
                {
                    // Write from the source to the destination.
                    bulkcopy.WritetoServer(reader);
                }
                catch (Exception ex)
                {
                    //stuff
                }
                finally
                {
                    //stuff removed for this post
                }
            }
        }
    }

    remoteConnection.Close();
}
return true;

我知道这可能会受到sql注入等的影响,但这个应用程序仅供我使用而不是此处的问题.

我究竟做错了什么?

编辑

我检查了reader的值(var reader = commandSourceData.ExecuteReader();)并且它有我想要的条目,这意味着他从遥控器读取是正常的.

解决方法

bulkcopy.DestinationTableName = table;
bulkcopy.WritetoServer(reader);

这些线是错的它应该看起来像这样..

bulkcopy.DestinationTableName = "dbo." + DataTable.TableName; 
bulkcopy.WritetoServer(DataTable);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐