我试图使用bulkCopy.WriteToServer()将数据从远程Server复制到本地Server,但从未将任何数据写入本地Server数据库。
代码立即跳过WriteToServer()方法..。我不知道它的内部是否有故障,也没有显示错误信息
我读过How to duplicate a SQL Server 2000 table programatically using .NET 2.0?,我正在使用非常类似的代码。虽然我在远程和本地Server 2014 Express上使用SQLServer2008Express:
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();)的值,它有我所期望的条目,这意味着他从远程阅读是可以的。
发布于 2015-05-28 19:08:52
bulkCopy.DestinationTableName = table;
bulkCopy.WriteToServer(reader);这些线条是错的,应该是这样的..
bulkCopy.DestinationTableName = "dbo." + DataTable.TableName;
bulkCopy.WriteToServer(DataTable);https://stackoverflow.com/questions/30109838
复制相似问题