 |
|
วิธีการเรียกใช้ Class (Connection String) จากไฟล์อื่นครับ |
|
 |
|
|
 |
 |
|
อยู่ที่ว่าจะเก็บไว้ที่ไหนครับ เช่น (WinApp) ผมเก็บไว้ในไฟล์ ini แล้วตัว Connection String ก็เก็บไว้ในโมดูลประกาศเป็น Public หรือ Friend
จะมีฟังก์ชั่นสำหรับอ่านไฟล์ ini
Code (VB.NET)
Module modINI
Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Int32
Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
''' <summary>
''' Write INI File writeIni(FilePath, Section, ParameterName, Value)
''' </summary>
''' <param name="iniFileName">File Path</param>
''' <param name="Section">Section Name</param>
''' <param name="ParamName">Parameter Name</param>
''' <param name="ParamVal">Values</param>
''' <remarks></remarks>
Public Sub writeIni(ByVal iniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamVal As String)
Dim Result As Integer = WritePrivateProfileString(Section, ParamName, ParamVal, iniFileName)
End Sub
''' <summary>
''' Read INI File : TextBox1.Text = ReadIni(FilePath, Section, ParameterName, "")
''' </summary>
''' <param name="IniFileName">File Path</param>
''' <param name="Section">Section Name</param>
''' <param name="ParamName">Parameter Name</param>
''' <param name="ParamDefault">Return Values</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ReadIni(ByVal IniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamDefault As String) As String
Dim ParamVal As String = Space$(1024)
Dim LenParamVal As Long = GetPrivateProfileString(Section, ParamName, ParamDefault, ParamVal, Len(ParamVal), IniFileName)
ReadIni = Left$(ParamVal, LenParamVal)
End Function
End Module
จากนั้นก็อ่านค่า จากไฟล์ ini มาไว้ในตัวแปร
Code (VB.NET)
Friend FileConfigINI_mcode = Application.StartupPath + "\CONFIG.ini"
Friend server As String = ReadIni(FileConfigINI_mcode, "database_config", "server", "")
Friend database As String = ReadIni(FileConfigINI_mcode, "database_config", "data_base_name", "")
Friend user As String = ReadIni(FileConfigINI_mcode, "database_config", "data_base_user", "")
Friend password As String = ReadIni(FileConfigINI_mcode, "database_config", "data_base_password", "")
Friend port As String = ReadIni(FileConfigINI_mcode, "database_config", "data_base_port", "")
จากนั้นก็เอาค่าตัวแปรมาใส่ใน Connection String
Code (VB.NET)
Public data As String = "Server='" & server & "';Port='" & port & "';Database='" & database & "';Uid='" & user & "';Pwd='" & password & "';Convert Zero Datetime=True;"
Public ConnectionDB As New MySqlConnection(data)
|
ประวัติการแก้ไข 2017-06-01 08:00:11
 |
 |
 |
 |
Date :
2017-06-01 07:54:30 |
By :
pakyaudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|