简体   繁体   English

如何读取config.ini文件中的连接字符串

[英]how to read connection string in config.ini file

i have an config.ini file in my project this is the data save in my ini file shown below. 我在我的项目中有一个config.ini文件,这是保存在我的ini文件中的数据,如下所示。 config where DESKTOP-UAGO7SB\\SQLEXPRESS is the data source, admin is the uid and xxxx is the pwd i have a following code to read the data in config.ini file in my module 配置 ,其中DESKTOP-UAGO7SB \\ SQLEXPRESS是数据源,admin是uid,xxxx是pwd我有以下代码来读取模块中config.ini文件中的数据

Public Function checkServer() As Boolean
    Try

        With frmSettings
            .OpenFileDialog1.FileName = Application.StartupPath & "\Config.ini"
            openedFileStream = .OpenFileDialog1.OpenFile()
        End With

        ReDim dataBytes(openedFileStream.Length - 1) 'Init 
        openedFileStream.Read(dataBytes, 0, openedFileStream.Length)
        openedFileStream.Close()
        tmpStr = System.Text.Encoding.Unicode.GetString(dataBytes)

        With frmSettings
            If Split(tmpStr, ":")(4) = "1" Then
                'network
                CnString = "Data Source=" & Split(tmpStr, ":")(0) & _
                           ";database=DBMISO" & _
                           ";uid=" & Split(tmpStr, ":")(2) & _
                           ";pwd=" & Split(tmpStr, ":")(3)

            Else
                'local
                'MsgBox(Split(tmpStr, ":")(1))
                CnString = "Data Source=" & Split(tmpStr, ":")(1) & _
                           ";Database=DBMISO; Trusted_Connection=yes;"
            End If
        End With
        Dim sqlCon As New SqlConnection
        sqlCon.ConnectionString = CnString
        sqlCon.Open()
        checkServer = True
        sqlCon.Close()
    Catch ex As Exception
        checkServer = False
    End Try
End Function

whenever i call the CnString to connect the database it gives me an error 每当我调用CnString连接数据库时,都会给我一个错误

The ConnectionString property has not been initialized ConnectionString属性尚未初始化

A lot of things don't make sense in your code or are plain wrong. 许多事情在您的代码中没有意义或完全是错误的。
You are working with NET so you should use the Framework classes and methods not the old Visual Basic function maintained just for compatibility 您正在使用NET,因此应使用Framework类和方法,而不要使用仅出于兼容性而保留的旧Visual Basic函数。

Try
    ' Read all the text from your config file
    Dim configData = File.ReadAllText(Application.StartupPath & "\Config.ini")
    ' Split it once and for all
    Dim configParts = configData.Split(":"c)

    ' You expect your config to have at least 4 parts
    if configParts.Length >= 4 Then

        ' Arrays start at zero so the 1 is at index 3 not 4
        If configParts(3) = "1" Then

            ' Again, arrays start at index zero.....
            CnString = "Data Source=" & configParts(0) & _
                       ";database=DBMISO" & _
                       ";uid=" & configParts(1) & _
                       ";pwd=" & configParts(2)
        Else
            'local
            CnString = "Data Source=" & configParts(0) & _
                       ";Database=DBMISO; Trusted_Connection=yes;"
        End If

        ' Enclose in a using statement to dispose the connection also in case
        ' of exceptions
        Using sqlCon = New SqlConnection(CnString)
            sqlCon.Open()
            checkServer = True
        End Using
    Else
        MessageBox.Show("Incorrect config.ini")
    End If
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try
return checkServer

After all this effort perhaps you should consider that NET has a proven infrastructure to deal with connectionstrings and with configuration files. 经过所有这些努力,也许您应该考虑NET具有经过验证的基础结构来处理连接字符串和配置文件。 You should really stop using this approach and learn how to use a configuration file and how to handle a connection string from a config file 您应该真正停止使用这种方法,并学习如何使用配置文件以及如何处理配置文件中连接字符串。

Assuming that you have a string named "YourConnectionStringName", this should work: 假设您有一个名为“ YourConnectionStringName”的字符串,这应该可以工作:

Dim cnString As String = Web.Configuration.WebConfigurationManager.ConnectionStrings("YourConnectionStringName").ConnectionString

You will need to add a reference to System.Web. 您将需要添加对System.Web的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM