C# 跨非链接SQL Server批更新表
我正在尝试使用以下方法跨“非链接”SQL Server更新表: C#或VB.net和ADO.netC# 跨非链接SQL Server批更新表,c#,.net,sql-server,vb.net,ado.net,C#,.net,Sql Server,Vb.net,Ado.net,我正在尝试使用以下方法跨“非链接”SQL Server更新表: C#或VB.net和ADO.netSqlDataAdapter 我需要使用DataTable和SqlDataAdapter 非常重要:我需要使用BatchUpdate并避免在数据表中循环 服务器1中的表设计与服务器2中的表设计不同 源表: Server 1. Table 1 ID INT NAME Varchar(30) Date DateTime Server 2. Table 2 ID INT TableOneId INT (
SqlDataAdapter
我需要使用DataTable
和SqlDataAdapter
非常重要:我需要使用BatchUpdate
并避免在数据表中循环
服务器1中的表设计与服务器2中的表设计不同
源表:
Server 1. Table 1
ID INT
NAME Varchar(30)
Date DateTime
Server 2. Table 2
ID INT
TableOneId INT (Foreign Key from Server 1. Table 1)
NAME Varchar(30)
Date DateTime
目的地表:
Server 1. Table 1
ID INT
NAME Varchar(30)
Date DateTime
Server 2. Table 2
ID INT
TableOneId INT (Foreign Key from Server 1. Table 1)
NAME Varchar(30)
Date DateTime
我需要一个关于如何使用SqlDataAdapter
或其他批处理方法更新服务器2上的表2的示例。由于数据表具有不同的架构,您必须循环数据表中的每一行,以使其正确地从一行移植到另一行
如果是我,那将是我最不关心的事情。那很快。最大的问题是内存使用和网络速度
您必须有两个数据表、两个数据适配器和两次数据库访问(每台服务器一次)。那有很多工作要做。循环应该不那么令人担忧
(尽管我同意在服务器2的DataTable上进行批量更新是明智的。)由于您的DataTable具有不同的模式,您必须循环遍历DataTable中的每一行,以使其正确地从一行移植到另一行
如果是我,那将是我最不关心的事情。那很快。最大的问题是内存使用和网络速度
您必须有两个数据表、两个数据适配器和两次数据库访问(每台服务器一次)。那有很多工作要做。循环应该不那么令人担忧
(尽管我同意在服务器2的数据表上进行批量更新是明智的。)您应该将SqlDataAdapter的属性设置为0(无限制)。
我看不到不循环表1就更新表2的方法
下面是一个示例代码,向您展示实现此目标的一种方法:
Public Sub BatchUpdate(ByVal table1 As DataTable)
Dim connectionStringServer2 As String = GetConnectionString()
Using connection As New SqlConnection(connectionStringServer2)
Dim adapter As New SqlDataAdapter()
'Set the UPDATE command and parameters'
adapter.UpdateCommand = New SqlCommand( _
"UPDATE Table2 SET " _
& "NAME=@NAME,Date=@Date WHERE TableOneId=@TableOneId;", _
connection)
adapter.UpdateCommand.Parameters.Add("@Name", _
SqlDbType.NVarChar, 50, "Name")
adapter.UpdateCommand.Parameters.Add("@Date", _
SqlDbType.DateTime, 0, "Date")
adapter.UpdateCommand.Parameters.Add("@TableOneId", _
SqlDbType.Int, 0, "TableOneId")
adapter.UpdateCommand.UpdatedRowSource = _
UpdateRowSource.None
' Set the batch size,'
' try to update all rows in a single round-trip to the server'
adapter.UpdateBatchSize = 0
Dim table2 As New DataTable("table2")
table2.Columns.Add(New DataColumn("Name", GetType(String)))
table2.Columns.Add(New DataColumn("Date", GetType(Date)))
table2.Columns.Add(New DataColumn("TableOneId", GetType(Int32)))
' copy content from table1 to table2'
For Each row As DataRow In table1.Rows
Dim newRow = table2.NewRow
newRow("TableOneId") = row("ID")
newRow("Name") = row("Name")
newRow("Date") = row("Date")
table2.Rows.Add(newRow)
' note: i have not tested following, but it might work or give you a clue'
newRow.AcceptChanges()
newRow.SetModified()
Next
' Execute the update'
adapter.Update(table2)
End Using
End Sub
您应该将SqlDataAdapter的属性设置为0(无限制)。
我看不到不循环表1就更新表2的方法
下面是一个示例代码,向您展示实现此目标的一种方法:
Public Sub BatchUpdate(ByVal table1 As DataTable)
Dim connectionStringServer2 As String = GetConnectionString()
Using connection As New SqlConnection(connectionStringServer2)
Dim adapter As New SqlDataAdapter()
'Set the UPDATE command and parameters'
adapter.UpdateCommand = New SqlCommand( _
"UPDATE Table2 SET " _
& "NAME=@NAME,Date=@Date WHERE TableOneId=@TableOneId;", _
connection)
adapter.UpdateCommand.Parameters.Add("@Name", _
SqlDbType.NVarChar, 50, "Name")
adapter.UpdateCommand.Parameters.Add("@Date", _
SqlDbType.DateTime, 0, "Date")
adapter.UpdateCommand.Parameters.Add("@TableOneId", _
SqlDbType.Int, 0, "TableOneId")
adapter.UpdateCommand.UpdatedRowSource = _
UpdateRowSource.None
' Set the batch size,'
' try to update all rows in a single round-trip to the server'
adapter.UpdateBatchSize = 0
Dim table2 As New DataTable("table2")
table2.Columns.Add(New DataColumn("Name", GetType(String)))
table2.Columns.Add(New DataColumn("Date", GetType(Date)))
table2.Columns.Add(New DataColumn("TableOneId", GetType(Int32)))
' copy content from table1 to table2'
For Each row As DataRow In table1.Rows
Dim newRow = table2.NewRow
newRow("TableOneId") = row("ID")
newRow("Name") = row("Name")
newRow("Date") = row("Date")
table2.Rows.Add(newRow)
' note: i have not tested following, but it might work or give you a clue'
newRow.AcceptChanges()
newRow.SetModified()
Next
' Execute the update'
adapter.Update(table2)
End Using
End Sub
如果您使用的是SQLServer2008或更高版本,则使用SqlDataAdapter的替代方法可能是使用表值参数:
因此,步骤如下:
1) 将源数据读入数据表
2) 在将执行导入的目标数据库中写入新的存储过程。这将采用表值参数作为其参数之一。将在步骤1)中创建的数据表传递到此存储过程
优点:完全控制进口过程。可能比使用SqlDataAdapter的性能更好。e、 g.您可以编写T-SQL来一次性更新整个表
缺点:需要编写一些T-SQL(如果您对此感到满意,这可能不是问题)。如果您使用的是SQLServer 2008或更高版本,使用SqlDataAdapter的另一种选择可能是使用表值参数:
因此,步骤如下:
1) 将源数据读入数据表
2) 在将执行导入的目标数据库中写入新的存储过程。这将采用表值参数作为其参数之一。将在步骤1)中创建的数据表传递到此存储过程
优点:完全控制进口过程。可能比使用SqlDataAdapter的性能更好。e、 g.您可以编写T-SQL来一次性更新整个表
缺点:需要编写一些T-SQL(如果您对此感到满意,可能不会有问题).是否要避免循环数据表或避免多次往返数据库?我想使用BatchUpdate,因为处理性能问题是正确的做法。是否要避免循环数据表或避免多次往返数据库?我想使用BatchUpdate,因为处理性能问题是正确的做法。转到和提交同样的答案,我会给你奖金。@Internet:好的,我已经在你的其他问题上发布了这个答案,并在那里删除了我的其他方法。转到并提交同样的答案,我会给你奖金。@Internet:好的,我也在你的其他问题上发布了这个答案,并在那里删除了我的其他方法。