Imports System.Data.OleDb
Module MyModule
' ********************************************************
' 接続 / 読み込み / 更新
' ********************************************************
Sub Main()
' Oracle 接続用オブジェクト
Dim myCon As New OleDbConnection()
' Oracle コマンド用オブジェクト
Dim myCommand As New OleDbCommand()
' リーダ
Dim myReader As OleDbDataReader
' SQL文字列格納用
Dim Query As String = Nothing
Dim DBPath As String = _
System.IO.Path.GetDirectoryName( System.Windows.Forms.Application.ExecutablePath ) _
+ "\" + "販売管理C.mdb"
' 接続文字列の作成
myCon.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & DBPath & ";"
' *******************************************
' 接続
' *******************************************
Try
' 接続文字列を使用して接続
myCon.Open()
' コマンドオブジェクトに接続をセット
myCommand.Connection = myCon
' コマンドを通常 SQL用に変更
myCommand.CommandType = System.Data.CommandType.Text
Catch ex As Exception
Console.WriteLine(ex.Message)
Return
End Try
Query = "select * from 社員マスタ"
myCommand.CommandText = Query
' *******************************************
' レコードセット取得
' *******************************************
Try
myReader = myCommand.ExecuteReader
Catch ex As Exception
myCon.Close()
Console.WriteLine(ex.Message)
Return
End Try
' 列数
Dim nCols As Integer = myReader.FieldCount
Dim fldType As System.Type
' カラムループ用
Dim idx As Integer
Do While myReader.Read()
For idx = 0 To nCols - 1
if idx <> 0 then
Console.Write(",")
end if
' NULL でない場合
If Not myReader.IsDBNull(idx) Then
fldType = myReader.GetFieldType(idx)
If fldType.Name = "String" Then
Console.Write(myReader.GetValue(idx) + "")
ElseIf fldType.Name = "Int32" Then
Console.Write(myReader.GetInt32(idx).ToString() + "")
ElseIf fldType.Name = "DateTime" Then
Console.Write(myReader.GetDateTime(idx).ToString() + "")
Else
Console.Write(myReader.GetValue(idx).ToString() + "")
End If
Else
Console.Write("")
End If
Next
' 行の最後
Console.WriteLine("")
Loop
' リーダを使い終わったので閉じる
myReader.Close()
' *******************************************
' 更新 SQL 実行
' *******************************************
Query = "update 社員マスタ set 生年月日 = '2000/01/01' where 生年月日 is NULL"
myCommand.CommandText = Query
Try
myCommand.ExecuteNonQuery()
Catch ex As Exception
myCon.Close()
Console.WriteLine(ex.Message)
Return
End Try
' *******************************************
' 接続解除
' *******************************************
myCon.Close()
End Sub
End Module