ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文

  メンテナンス 前画面に戻る

対象スレッド 件名: VB.NET+Oracle ( 最もシンプルなソースコード )
名前: lightbox
処理選択
パスワード

件名 VB.NET+Oracle ( 最もシンプルなソースコード )
名前 lightbox
コメント
@DIV
Imports System.Data.OracleClient

Module MyModule

' ********************************************************
' * 実行
' ********************************************************
Sub Main()

	Dim myCon As OracleConnection = New OracleConnection()
	Dim myCommand As OracleCommand = New OracleCommand()
	Dim myReader As OracleDataReader = Nothing

	Dim sv As String = "night/xe"
	Dim user As String = "lightbox"
	Dim pass As String = "lightbox"

	Dim	myConnectString As String = _
		 "Server=" + sv + ";" + _
		 "User ID=" + user + ";" + _
		 "Password=" + pass + ";"

	myCon.ConnectionString = myConnectString

' **************************************
' * 接続
' **************************************
	Console.WriteLine("<pre>処理を開始しました")

	Try
		myCon.Open()
		myCommand.Connection = myCon
	Catch ex As Exception
		myCon = Nothing
		Console.WriteLine("接続エラーです")
		Console.WriteLine("接続文字列=>" + myConnectString)
		Console.WriteLine("システムのメッセージ=" + ex.Message)
		Console.WriteLine("</pre>")
		Exit Sub
	End Try

	Console.WriteLine("接続されました")


' **************************************
' * レコードセット取得
' **************************************
	Dim Query As String = "select * from 社員マスタ"

	myCommand.CommandText = Query

	Try
		myReader = myCommand.ExecuteReader()
	Catch ex As Exception
		Console.WriteLine("OracleDataReader 作成エラーです")
		Console.WriteLine("システムのメッセージ=" + ex.Message)

		myCon.Close()
		Console.WriteLine("接続を閉じました")
		Console.WriteLine("</pre>")
		Exit Sub
	End Try

' **************************************
' * データ取得
' **************************************
	Dim bRead As Boolean = myReader.Read()
	Dim nField As Integer = myReader.FieldCount
	Dim nIdx As Integer
	Dim strField As String

	Console.WriteLine("<table>")
	Do While bRead

		Console.WriteLine("<tr>")

		For nIdx = 0 to nField - 1
			if myReader.IsDBNull(nIdx) then
				strField = "<td></td>"
			else
				strField = "<td>" + myReader.GetValue(nIdx).ToString() + "</td>"
			end if
			Console.Write(strField)
		Next
		Console.WriteLine("")

		Console.WriteLine("</tr>")

		bRead = myReader.Read()
	Loop
	Console.WriteLine("</table>")


' **************************************
' * 接続解除
' **************************************
	' 読み取りオブジェクトが閉じていない場合は、閉じる
	If Not myReader Is Nothing Then
		If Not myReader.IsClosed Then
			myReader.Close()
			Console.WriteLine("OracleDataReader を閉じました")
		End If
	End If

	If Not myCon Is Nothing Then
		' 接続を閉じる
		If myCon.State = System.Data.ConnectionState.Open Then
			myCon.Close()
			Console.WriteLine("接続を閉じました")
		End If
	End If

	Console.WriteLine("処理を終了しました</pre>")

End Sub

End Module
@END