首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bulkCopy.WriteToServer不复制

bulkCopy.WriteToServer不复制
EN

Stack Overflow用户
提问于 2015-05-07 19:17:50
回答 1查看 2.3K关注 0票数 4

我试图使用bulkCopy.WriteToServer()将数据从远程Server复制到本地Server,但从未将任何数据写入本地Server数据库。

代码立即跳过WriteToServer()方法..。我不知道它的内部是否有故障,也没有显示错误信息

我读过How to duplicate a SQL Server 2000 table programatically using .NET 2.0?,我正在使用非常类似的代码。虽然我在远程和本地Server 2014 Express上使用SQLServer2008Express:

代码语言:javascript
复制
using (SqlConnection remoteConnection = new SqlConnection(remoteConnectionString))
{
    var query = "SELECT * FROM information_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注入等的影响,但这个应用程序只供我使用,而不是这里的问题。

我做错了什么?

编辑

我检查了读取器(var reader = commandSourceData.ExecuteReader();)的值,它有我所期望的条目,这意味着他从远程阅读是可以的。

EN

回答 1

Stack Overflow用户

发布于 2015-05-28 19:08:52

代码语言:javascript
复制
bulkCopy.DestinationTableName = table;
bulkCopy.WriteToServer(reader);

这些线条是错的,应该是这样的..

代码语言:javascript
复制
bulkCopy.DestinationTableName = "dbo." + DataTable.TableName; 
bulkCopy.WriteToServer(DataTable);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30109838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档