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=社員コードを入力して下さい
  




yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[dnettool]
Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
24/04/20 04:18:42
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube