gpt4 book ai didi

c# - bulkCopy.WriteToServer 不复制

转载 作者:行者123 更新时间:2023-11-30 12:43:51 24 4
gpt4 key购买 nike

我正在尝试使用 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 2008 Express 到 SQL Server 2014 Express:

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 注入(inject)等的影响,但这个应用程序仅供我使用,而不是这里的问题。

我做错了什么?

编辑

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

最佳答案

bulkCopy.DestinationTableName = table;
bulkCopy.WriteToServer(reader);

这些行是错误的,它应该看起来像这样..

bulkCopy.DestinationTableName = "dbo." + DataTable.TableName; 
bulkCopy.WriteToServer(DataTable);

关于c# - bulkCopy.WriteToServer 不复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30109838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com