如何将 table 索引复制到 ado.net 中的另一个 table
How to copy table index to another table in ado.net
我在 ado.net 中使用 SqlBulkCopy 将一个 table 复制到另一个。代码是:
using (sourceConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["OTPTestConnectionString"].ConnectionString))
{
sourceConnection.Open();
// Get data from the source table as a SqlDataReader.
SqlCommand commandSourceData = new SqlCommand("SELECT * FROM " + sourceTableName, sourceConnection);
SqlDataReader reader = commandSourceData.ExecuteReader();
using (destConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLExpressDatabaseConnectionString"].ConnectionString))
{
destConnection.Open();
// Perform bulk copy
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destConnection))
{
bulkCopy.DestinationTableName = destTableName;
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
}
reader.Close();
...
工作正常,但无法将原始 table 中的主键索引复制到新的 table。我想知道如何在 Ado.net 中做到这一点。谢谢
我用 SqlBulkCopyOptions.KeepIdentity 弄明白了。代码是
SqlBulkCopyOptions options = SqlBulkCopyOptions.KeepIdentity;
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destConnection.ConnectionString, options))
{
bulkCopy.DestinationTableName = destTableName;
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
我在 ado.net 中使用 SqlBulkCopy 将一个 table 复制到另一个。代码是:
using (sourceConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["OTPTestConnectionString"].ConnectionString))
{
sourceConnection.Open();
// Get data from the source table as a SqlDataReader.
SqlCommand commandSourceData = new SqlCommand("SELECT * FROM " + sourceTableName, sourceConnection);
SqlDataReader reader = commandSourceData.ExecuteReader();
using (destConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLExpressDatabaseConnectionString"].ConnectionString))
{
destConnection.Open();
// Perform bulk copy
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destConnection))
{
bulkCopy.DestinationTableName = destTableName;
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
}
reader.Close();
...
工作正常,但无法将原始 table 中的主键索引复制到新的 table。我想知道如何在 Ado.net 中做到这一点。谢谢
我用 SqlBulkCopyOptions.KeepIdentity 弄明白了。代码是
SqlBulkCopyOptions options = SqlBulkCopyOptions.KeepIdentity;
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destConnection.ConnectionString, options))
{
bulkCopy.DestinationTableName = destTableName;
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}