'▼接続を確立 Dim cn1 As New SqlConnection cn1.ConnectionString = "Server=.;Integrated Security = True" cn1.Open() '▼クエリ生成 Dim sqlCm1 As New SqlCommand("SELECT * FROM sys.databases", cn1) '▼DataReader生成 Dim reader As SqlDataReader reader = sqlCm1.ExecuteReader '▼ループを回しつつ、同じ接続でExecuteNonQuery While reader.Read Dim sqlCm2 As New SqlCommand("SELECT * FROM sys.objects", cn1) 'ここでエラー sqlCm2.ExecuteNonQuery() End While '▼後処理 reader.Close() cn1.Close() MsgBox("OK")
'▼接続を確立 Dim cn1 As New SqlConnection cn1.ConnectionString = "Server=.;Integrated Security = True" cn1.Open() '▼クエリ生成 Dim sqlCm1 As New SqlCommand("SELECT * FROM sys.databases", cn1) '▼DataReader生成 Dim reader As SqlDataReader reader = sqlCm1.ExecuteReader '▼2つ目の接続を確立 Dim cn2 As New SqlConnection("Server=.;Integrated Security = True") cn2.Open() '▼1つ目の接続でループを回しつつ、2つ目の接続でExecuteNonQuery While reader.Read Dim sqlCm2 As New SqlCommand("SELECT * FROM sys.objects", cn2) sqlCm2.ExecuteNonQuery() End While '▼後処理 cn2.Close() reader.Close() cn1.Close() MsgBox("OK")
'▼接続を確立 Dim cn1 As New SqlConnection cn1.ConnectionString = "Server=.;Integrated Security = True" cn1.Open() '▼クエリ生成 Dim sqlCm1 As New SqlCommand("SELECT * FROM sys.databases", cn1) '▼DataTableにデータを格納する Dim adapter As New SqlDataAdapter(sqlCm1) Dim table As New DataTable adapter.Fill(table) '▼DataTableでループを回しつつ、1つ目の接続でExecuteNonQuery For Each row As DataRow In table.Rows Dim sqlCm2 As New SqlCommand("SELECT * FROM sys.objects", cn1) sqlCm2.ExecuteNonQuery() Next '▼後処理 cn1.Close() MsgBox("OK")