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 = "username"
.Password = "password"
.DataSource = "servername"
.InitialCatalog = "pubs"
End With
''コネクション
Using cn As New SqlConnection(builder.ConnectionString)
''コマンド
Using cmd As New SqlCommand("SELECT * FROM jobs", cn)
''データアダプタ
Using da As New SqlDataAdapter(cmd)
''データセット
Using ds As New DataSet
''データ取得
da.Fill(ds, "jobs")
''バルクコピー
Dim bc As New SqlBulkCopy(cn)
''コピー先テーブル名を指定
bc.DestinationTableName = "jobs_copy"
''コネクションオープン
cn.Open()
''コピー元をコピー先へ書き込み
bc.WriteToServer(ds.Tables(0))
End Using
End Using
End Using
End Using
MessageBox.Show("処理完了")
End Sub
End Class
|