VB.NETからSQL Serverに対してDELETE文、UPDATE文、INSERT文などの処理を行う。
Functionにしているので、処理結果が0の場合は処理なし、1の場合は処理確定した事が分かるよ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
Function SQLDB_Excute(ByVal MySQL As String, ByRef MyDB As String) As Integer Dim ret As Integer = 0 Try Dim con = New System.Data.SqlClient.SqlConnection() '------ Windows認証を利用して接続 ------ con.ConnectionString = _ "Data Source = USER-PC\AAASQLEXPRESS" & _ ";Initial Catalog = " & MyDB & _ ";Integrated Security = SSPI" & _ ";Connection Timeout=30;" con.Open() Dim SQLCMD As SqlClient.SqlCommand Debug.Print(MySQL) SQLCMD = con.CreateCommand SQLCMD.CommandText = MySQL SQLCMD.CommandTimeout = 0 ret += SQLCMD.ExecuteNonQuery 'If ret = 0 Then 'MsgBox("処理しました", MsgBoxStyle.Information, "Success") 'Else 'MsgBox("処理出来ませんでした。" & vbCr & vbCr & _ ' "再度実行して下さい。", MsgBoxStyle.Critical, "Error") 'End If con.Close() con.Dispose() Catch ex As Exception MessageBox.Show(ex.ToString) ret = 0 End Try Return ret End Function |