我的任务是创建一个VB应用程序,根据特定条件将SQL表从一个服务器移动到另一个服务器。这些表位于各种数据库中,每个表上的模式可能不同。
到目前为止,我是:
创建符合条件的表格列表
For Each table
一个。阅读当前架构
湾在另一个服务器上重新创建表
℃。从原始表中读取数据并将其写入新表
将会有更多步骤要遵循,但最后一步是我被困住的地方。
我正在从表中读取数据,调试时我传输的数据数组看起来不错。但是目标表永远不会收到数据,它总是空的。
以下是我正在努力解决的过程中的两个步骤。
Private Sub MoveData(dbName As String, TableName As String)
'Read Data from production server and move to Archive Server
Dim constring As String = "Data Source=CONCORDE;Initial Catalog=" & dbName & ";User ID=imhdata;Password=dataimh"
Using con As New SqlConnection(constring)
Using table As DataTable = New DataTable
Using command As SqlCommand = New SqlCommand("SELECT * FROM " & TableName, con)
Using adapter As SqlDataAdapter = New SqlDataAdapter(command)
adapter.Fill(table)
End Using
End Using
For Each row As DataRow In table.Rows
SaveInArchiveDB(TableName, table.Rows(0).ItemArray)
Next
End Using
con.Close()
End Using
End Sub
Private Sub SaveInArchiveDB(TableName As String, DataRow As Array)
Dim Query As String = "INSERT INTO " & TableName & "_Archive VALUES (@DataRow)"
Dim constr As String = "Data Source=ArchiveDB;Initial Catalog=ArchiveData;User ID=imhdata;Password=dataimh"
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(Query, con)
Try
con.Open()
cmd.Parameters.Add(New SqlParameter("@DataRow", DataRow))
cmd.ExecuteNonQuery()
Catch ex As Exception
IO.File.AppendAllText("/WO_Error_Logs/Log.txt", String.Format("{0}{1}{2}{3}", Environment.NewLine, Date.Now(), Environment.NewLine, ex.Message))
If con.State = ConnectionState.Open Then
con.Close()
End If
Exit Sub
Finally
con.Close()
End Try
End Using
End Using
End Sub