注册 登录
编程论坛 VB.NET论坛

vb.net如何从ini文件读取SQL的连接字符串

wenoxy 发布于 2018-12-20 15:45, 2983 次点击
如题,哪位大神指点一下,我想从一个ini文件里读取服务器的地址,sa用户名,密码,和连接数据库,而且可以通过自制的FROM进行修改和保存,麻烦哪位大神给个代码学习一下
3 回复
#2
厨师王德榜2019-01-08 16:05
我来抛个砖吧。
程序代码:

'聲明INI读写API函數
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
        (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
    Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
        (ByVal lpApplicationName As String, _
        ByVal lpKeyName As String, _
        ByVal lpString As String, _
        ByVal lpFileName As String) As Int32
' 用法:
    '读取INI
    Public Function GetINI( _
        ByVal Section As String, _
        ByVal AppName As String, _
        ByVal lpDefault As String, _
        ByVal FileName As String) As String
        Dim Str As String = ""
        Str = LSet(Str, 256)
        GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
        Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
    End Function
    '写入INI
    Public Function WriteINI( _
    ByVal section As String, _
    ByVal appName As String, _
    ByVal lpDefault As String, _
    ByVal fileName As String) As Long
        WriteINI = WritePrivateProfileString(section, appName, lpDefault, fileName)
    End Function
#3
moou2019-02-13 14:40
我来写个实例,仅当参考
程序代码:

    '用法示例
    'config.ini文件内容
    '[服务器]
    'IP=192.168.0.250
    '[映射盘符]
    '盘符=Y:

        '判断读是否读取到取配置文件信息,并读取IP信息
        If GetINI("服务器", "IP", "", Application.StartupPath & "\config.ini") = "" Or _
           GetINI("映射盘符", "盘符", "", Application.StartupPath & "\config.ini") = "" Then
            MsgBox("配置文件错误,请进去设置界面进行设置!!!", , "国智网络")
        Else
            ReadIP = GetINI("服务器", "IP", "", Application.StartupPath & "\config.ini")
        End If

        '写入IP到配置文件
        Dim write1 As Long
        write1 = WriteINI("服务器", "IP", "192.168.0.251", Application.StartupPath & "\config.ini")
#4
wenoxy2019-08-18 20:47
一楼看明白,二楼的没看明白
1