データベースを VB に接続する際に使用するこれら 2 つの機能またはその背後にある原理を誰か説明してもらえますか? そのコードは私たちの先生からのもので、先生はなぜそのようにコーディングする必要があるのかについてあまり説明しませんでした。DBにはMS Access 2003ファイルを使用しています。
Imports System.Data.OleDb
Public Class Form1
Dim dbPath As String = Application.StartupPath & "\Database\xxxx.mdb"
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE = " & dbPath
Private Function performQuery(ByRef connectionString As String, ByVal sqlCommand As String) As OleDb.OleDbDataReader
Dim dbConnection As OleDbConnection
Dim dbCommand As New OleDbCommand()
Dim dbDataReader As OleDb.OleDbDataReader = Nothing
Try
dbConnection = New OleDbConnection(connectionString)
dbCommand.CommandText = sqlCommand
dbCommand.Connection = dbConnection
dbConnection.Open()
dbDataReader = dbCommand.ExecuteReader
Return dbDataReader
dbConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
Return Nothing
End Function
Private Function performNonQuery(ByVal connectionString As String, ByVal sqlCommand As String) As Boolean
Dim dbConnection As OleDbConnection
Dim dbCommand As New OleDbCommand()
Try
dbConnection = New OleDbConnection(connectionString)
dbCommand.CommandText = sqlCommand
dbCommand.Connection = dbConnection
dbCommand.Connection.Open()
dbCommand.ExecuteNonQuery()
dbConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
End Try
Return True
End Function
End Class