2018年04月30日
VB.NET SQLServer SQL文実行
SQL分を実行する(UPDATE INSERT DELETE)
Public Function SQLExec(GetSQLStatement As String) As Boolean
SQLExec = False
Try
Dim stConnectionString As String
Dim strServer As String
strServer = "サーバ名"
If strServer = "" Then
MsgBox("接続サーバを入力してください。")
Exit Function
End If
Dim sDBSever As String
Dim sDBName As String
Dim sLoginID As String
Dim sPassWD As String
sDBSever = strServer & "\SQLEXPRESS"
sDBName = "TEST"
sLoginID = "XX"
sPassWD = "PPPPPPPP"
'接続文字列
stConnectionString = "Provider=Sqloledb;Data Source=" & sDBSever _
& ";Initial Catalog=" & sDBName _
& ";Connect Timeout=15" _
& ";user id=" & sLoginID _
& ";password=" & sPassWD _
& ""
'----------------------------------------------------
' ADODB.Connection の新しいインスタンスを生成する
'----------------------------------------------------
Dim cAdoConnection As ADODB.Connection
cAdoConnection = New ADODB.Connection
'----------------------------------------------------
' データベース接続を開く
'----------------------------------------------------
Call cAdoConnection.Open(stConnectionString)
Dim strSQL As String
'Dim LikeStr As String
'LikeStr = "%"
strSQL = GetSQLStatement
Dim cRecordset As New ADODB.Recordset
Call cRecordset.Open(strSQL, cAdoConnection, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockPessimistic)
'cRecordset.Update()
L_finally:
' レコードセットを閉じる (正しくは オブジェクトの破棄を保証する を参照)
If Not cRecordset Is Nothing Then
cRecordset = Nothing
End If
If Not cAdoConnection Is Nothing Then
cAdoConnection = Nothing
End If
SQLExec = True
Catch ex As Exception
SQLExec = False
End Try
End Function
Public Function SQLExec(GetSQLStatement As String) As Boolean
SQLExec = False
Try
Dim stConnectionString As String
Dim strServer As String
strServer = "サーバ名"
If strServer = "" Then
MsgBox("接続サーバを入力してください。")
Exit Function
End If
Dim sDBSever As String
Dim sDBName As String
Dim sLoginID As String
Dim sPassWD As String
sDBSever = strServer & "\SQLEXPRESS"
sDBName = "TEST"
sLoginID = "XX"
sPassWD = "PPPPPPPP"
'接続文字列
stConnectionString = "Provider=Sqloledb;Data Source=" & sDBSever _
& ";Initial Catalog=" & sDBName _
& ";Connect Timeout=15" _
& ";user id=" & sLoginID _
& ";password=" & sPassWD _
& ""
'----------------------------------------------------
' ADODB.Connection の新しいインスタンスを生成する
'----------------------------------------------------
Dim cAdoConnection As ADODB.Connection
cAdoConnection = New ADODB.Connection
'----------------------------------------------------
' データベース接続を開く
'----------------------------------------------------
Call cAdoConnection.Open(stConnectionString)
Dim strSQL As String
'Dim LikeStr As String
'LikeStr = "%"
strSQL = GetSQLStatement
Dim cRecordset As New ADODB.Recordset
Call cRecordset.Open(strSQL, cAdoConnection, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockPessimistic)
'cRecordset.Update()
L_finally:
' レコードセットを閉じる (正しくは オブジェクトの破棄を保証する を参照)
If Not cRecordset Is Nothing Then
cRecordset = Nothing
End If
If Not cAdoConnection Is Nothing Then
cAdoConnection = Nothing
End If
SQLExec = True
Catch ex As Exception
SQLExec = False
End Try
End Function
【このカテゴリーの最新記事】
-
no image
-
no image
-
no image
-
no image
-
no image
-
no image
-
no image
-
no image
-
no image
この記事へのコメント
コメントを書く
この記事へのトラックバックURL
https://fanblogs.jp/tb/7604236
※ブログオーナーが承認したトラックバックのみ表示されます。
この記事へのトラックバック