Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
''SQL接続文字(Settingなどを利用しましょう)
Dim builder As New SqlConnectionStringBuilder
With builder
.UserID = "sa"
.Password = ""
.DataSource = "venus"
.InitialCatalog = "pubs"
End With
''コネクション
Dim cn As New SqlConnection(builder.ConnectionString)
''コマンド
Dim cmd As New SqlCommand("SELECT * FROM jobs", cn)
''データアダプタ
Dim da As New SqlDataAdapter(cmd)
''データセット
Dim ds As New DataSet
''データ取得
da.Fill(ds, "jobs")
''バルクコピー
Dim bc As New SqlBulkCopy(cn)
''コピー先テーブル名を指定
bc.DestinationTableName = "jobs_copy"
''コネクションオープン
cn.Open()
''コピー元・先のフィールド割付
bc.ColumnMappings.Add("job_id", "job_id") 'そのまま
bc.ColumnMappings.Add("job_desc", "job_desc") 'そのまま
bc.ColumnMappings.Add("min_lvl", "max_lvl") 'min_lvl -> max_lvl
bc.ColumnMappings.Add("max_lvl", "min_lvl") 'max_lvl -> min_lvl
''コピー元をコピー先へ書き込み
bc.WriteToServer(ds.Tables(0))
''後始末
bc.Close()
ds.Dispose()
da.Dispose()
cmd.Dispose()
cn.Close()
cn.Dispose()
MessageBox.Show("処理完了")
End Sub
End Class
|