我正在寻找完成以下任务的最佳方法的建议。我需要运行一个最多可以提取 200 行的 SQL 查询。对于每一行,我需要从该行获取数据并将其插入另一个表中。起初我以为我会像下面列出的代码示例那样嵌套数据库连接。但是,我想知道这是否是不正确的做法以及更好的方法。
有任何想法吗?
Dim dbconn As New SqlConnection
Dim dbconn2 As New SqlConnection
dbconn = New SqlConnection(ConfigurationManager.ConnectionStrings("databaseName").ConnectionString
dbconn2 = New SqlConnection(ConfigurationManager.ConnectionStrings("databaseName").ConnectionString
dbconn.Open()
Dim cmd As New SQLCommand
Dim cmd2 As New SQLCommand
Dim UserReader As SqlDataReader
Dim cmdStringSource As String = "SELECT approx 25 fields WHERE fldRandomField = 26"
Dim cmdStringUpdate As String
cmd = New SQLCommnd(cmdStringSource, dbconn)
UserReader = cmdExecuteReader()
If UserReader.HasRows Then
While UserReader.Read()
cmdStringUpdate = "UPDATE tblUpdate SET a whole bunch of stuff from the cmdStringSource current record"
dbconn2.Open()
cmd2=New SQLCommand(cmdStringUpdate, dbconn2)
cmd2.ExecuteNonQuery()
dbconn2.close()
End While
End If
UserReader.Close()
dbconn.close()