VB.net : ini ファイル処理クラス

  主に、データベースの接続情報を設定する為に作成



※ 少なくとも MySQL と Oracle での接続を想定しています

以下は DB 用です
GetDBType
GetServer
GetDB
GetUser
GetPass

以下は一般用です
GetValue
GetErrorMessage
DispError

MySQL の接続では、サービス名より起動されていない場合はアプリケーションから起動する事を想定しています。
( 一般用メソッドで対応 )

ini の例
  
[MTN]
ConnetctType=1

[MySQL]
Service=MySQL51
Server=localhost
Database=lightbox
User=root
Password=

[Oracle]
;Server=localhost/ORCL
Server=localhost/XE
User=lightbox
Password=lightbox
  


  
Public Class INI


    Public ini As String

    ' ******************************************************
    ' ini ファイル読み込み
    ' ******************************************************
    <DllImport("Kernel32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function GetPrivateProfileString( _
                ByVal lpAppName As String, _
                ByVal lpKeyName As String, _
                ByVal lpDefault As String, _
                ByVal lpReturnedString As StringBuilder, _
                ByVal nSize As Integer, _
                ByVal lpFileName As String) As Integer
    End Function

    ' ********************************************************
    ' (コンストラクタの定義)( Sub で定義する )
    ' ********************************************************
    Public Sub New(ByVal path As String)

        ini = path

    End Sub

    ' ******************************************************
    ' 接続 DB の種類を取得
    ' ******************************************************
    Public Function GetDBType() As Integer

        Dim str As StringBuilder = New StringBuilder(512)

        Call GetPrivateProfileString("MTN", "ConnetctType", "1", str, 512, ini)
        GetDBType = Integer.Parse(str.ToString())

    End Function

    ' ******************************************************
    ' 接続 Server を取得
    ' ******************************************************
    Public Function GetServer() As String

        Dim str As StringBuilder = New StringBuilder(512)
        Dim nType As Integer = GetDBType()

        Select Case nType
            Case 1
                Call GetPrivateProfileString("MySQL", "Server", "localhost", str, 512, ini)
            Case 2
                Call GetPrivateProfileString("Oracle", "Server", "localhost/ORCL", str, 512, ini)

        End Select

        GetServer = str.ToString()

    End Function

    ' ******************************************************
    ' 接続 Server を取得
    ' ******************************************************
    Public Function GetDB() As String

        Dim str As StringBuilder = New StringBuilder(512)
        Dim nType As Integer = GetDBType()

        Select Case nType
            Case 1
                Call GetPrivateProfileString("MySQL", "Database", "", str, 512, ini)
            Case 2
                str.Append("")

        End Select

        GetDB = str.ToString()

    End Function

    ' ******************************************************
    ' 接続 Server を取得
    ' ******************************************************
    Public Function GetUser() As String

        Dim str As StringBuilder = New StringBuilder(512)
        Dim nType As Integer = GetDBType()

        Select Case nType
            Case 1
                Call GetPrivateProfileString("MySQL", "User", "", str, 512, ini)
            Case 2
                Call GetPrivateProfileString("Oracle", "User", "", str, 512, ini)

        End Select

        GetUser = str.ToString()

    End Function

    ' ******************************************************
    ' 接続 Server を取得
    ' ******************************************************
    Public Function GetPass() As String

        Dim str As StringBuilder = New StringBuilder(512)
        Dim nType As Integer = GetDBType()

        Select Case nType
            Case 1
                Call GetPrivateProfileString("MySQL", "Password", "", str, 512, ini)
            Case 2
                Call GetPrivateProfileString("Oracle", "Password", "", str, 512, ini)

        End Select

        GetPass = str.ToString()

    End Function

    ' ******************************************************
    ' 一般取得
    ' ******************************************************
    Public Function GetValue(ByVal Section As String, ByVal Entry As String) As String

        Dim str As StringBuilder = New StringBuilder(512)
        Dim nType As Integer = GetDBType()

        Call GetPrivateProfileString(Section, Entry, "", str, 512, ini)

        GetValue = str.ToString()

    End Function

    ' ******************************************************
    ' エラーメッセージの取得
    ' ******************************************************
    Public Function GetErrorMessage(ByVal code As String) As String

        Dim str As StringBuilder = New StringBuilder(512)
        Dim nType As Integer = GetDBType()

        Call GetPrivateProfileString("Error", "E" & code, "", str, 512, ini)

        GetErrorMessage = str.ToString()

    End Function

    ' ******************************************************
    ' エラーメッセージの表示
    ' ******************************************************
    Public Sub DispError( _
        ByVal code As String, _
        Optional ByRef target As System.Windows.Forms.TextBox = Nothing, _
        Optional ByRef e As System.ComponentModel.CancelEventArgs = Nothing _
    )

        If Not target Is Nothing Then
            target.SelectAll()
            target.Focus()
        End If
        If Not e Is Nothing Then
            e.Cancel = True
        End If
        Dim str As String = Me.GetErrorMessage(code)
        MessageBox.Show(str & "   ", "R205", MessageBoxButtons.OK, _
	MessageBoxIcon.Exclamation)

    End Sub

End Class
  

関連する記事




  ini オブジェクトの利用サンプル



  
' INI ファイル処理オブジェクトのインスタンスを作成
ini = New INI(INI_PATH)
msg = New INI(INI_PATH_MESSAGE)

' サービスが実行されていない場合にサービスを実行
Dim sc As New ServiceController()
If ini.GetDBType() = "1" Then
    sc.ServiceName = ini.GetValue("MySQL", "Service")
    If sc.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
sc.Start()
    End If
End If

' DB 接続
db = New DB( _
    ini.GetDBType(), _
    ini.GetServer(), _
    ini.GetDB(), _
    ini.GetUser(), _
    ini.GetPass() _
)
db.Connect()
  

メッセージ用 ini ファイル
  
[Error]
E001=必須入力です
E002=数値を入力して下さい
E003=該当するコードは存在しません
E004=入力されたコードは既に存在します

E101=社員名は必須入力です
E102=基本給は必須入力です


[Info]
I001=社員コードを入力して下さい
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ