【问题标题】:VB.net connection string works, then doesn't, then does - why?VB.net 连接字符串有效,然后无效,然后有效 - 为什么?
【发布时间】:2021-08-08 05:46:17
【问题描述】:

使用 VB.NET,VS 19 v16.10.4

我有一个小应用程序,它要求用户提供数据库服务器、数据库名称,然后建立一个连接字符串。然后,使用我编写的数据访问层 DLL 运行检查以查看是否可以与数据库建立连接。

问题不寻常: 如果我将直接连接到数据访问层的连接字符串写入它连接的变量中;

如果我使用字符串构建器来构建字符串,然后将其传递给数据访问层,它会失败;

如果我将连接字符串写入变量,然后将其传递给数据访问层,它会失败;

如果我再次使用第一步,它会起作用。

 With DAL

            MessageBox.Show("Using hard-coded string")
            .ConnectionString = "data source=THEWINELIBRARY\MSSQLSERVER01;initial catalog=TrialDatabase;trusted_connection=true"
            .DoConnectionCheck() 'THIS WORKS


            MessageBox.Show("Using string builder string")
            .ConnectionString = SBConnString.ToString.Trim
            .DoConnectionCheck() 'THIS FAILS

            MessageBox.Show("Using CS string")
            .ConnectionString = CS.Trim
            .DoConnectionCheck() 'THIS FAILS

            MessageBox.Show("Using hard-coded string")
            .ConnectionString = "data source=THEWINELIBRARY\MSSQLSERVER01;initial catalog=TrialDatabase;trusted_connection=true"
            .DoConnectionCheck() 'THIS WORKS

        End With

数据访问层在 DoConnectionCheck 方法中做了以下工作:

 Public Sub DoConnectionCheck()
            OpenConnection()
            With AppConnection
                If (.State = ConnectionState.Open) Then
                    RaiseEvent ConnectionOpened()
                    CloseConnection()
               
                End If
            End With
        End Sub

以及 OpenConnection 方法:

Private Sub OpenConnection()
        With AppConnection
            'Is the conenction currently closed?
            If (.State = ConnectionState.Closed) Then
                Try
                    'Set connection string to POS
                    .ConnectionString = ConnectionString
                    'Try opening the connection
                    .Open()

                    'Otherwise raise an event.
                Catch E As Exception
                    RaiseEvent ConnectionFailed()

                End Try

            End If

        End With

    End Sub

而连接字符串是一个简单的属性:

 Public ConnectionString As String

所以,我不明白的是为什么连接失败,字符串生成器字符串和变量字符串,而不是硬编码字符串?

如果您需要任何其他代码,请告诉我,我发布了我认为足以解释问题的内容,而不会添加太多使问题无法阅读的内容。

这是从表单中的条目构造连接字符串的代码:

 Dim CS As String = "datasource=" & .TextServer.Text.Trim & ";initial catalog=" & .TextDatabase.Text.Trim & ";trusted_connection=true"

    

SBConnString = New StringBuilder
        With SBConnString
            .Clear()
            .Append("datasource=")
            .Append(Me.TextServer.Text.Trim)
            .Append(";initial catalog=")
            .Append(Me.TextDatabase.Text.Trim)
            .Append(";trusted_connection=true")
        End With

当这些字符串与硬编码字符串进行比较时

DAL.ConnectionString = "data source=THEWINELIBRARY\MSSQLSERVER01;initial catalog=TrialDatabase;trusted_connection=true"

该函数返回值 1,“第一个子字符串在排序顺序中位于第二个子字符串之后。”根据文档。

据我所知,这些字符串是相同的,但在表单文本框中必须插入一个额外的字符。

再次感谢任何建议。

一切顺利,

德莫特

【问题讨论】:

  • 仔细检查这些变量中的值:它们真的与硬编码的值相同吗?
  • 是的 - 在两个字符串中我有数据源,而不是数据源......因为你已经清除了我的一个愚蠢的错误,我明天会删除这个问题!谢谢,
  • 这是一个很好的例子,说明了为什么应该使用连接字符串构建器来构建连接字符串。对于 SQL Server,这将是 SqlConnectionstringBuilder。他们不能阻止你弄乱值,但至少他们可以阻止你弄乱字段名称。
  • 优秀的评论 - 谢谢。这是一门我以前没有遇到过的课程,所以它看起来正是我一开始想要的。
  • 一个简短的警告 - 如果你使用 SQLConnectionStringBuilder,你必须在传递它时使用 .ToLower - 它会将诸如数据源之类的单词大写,这可能会导致连接失败。

标签: vb.net


【解决方案1】:

过失 - 我有一个非常简单的错误,我看不到 - 在导致问题的两个字符串中,我有数据源而不是日期源......

【讨论】:

    猜你喜欢
    • 2015-07-31
    • 2020-08-02
    • 1970-01-01
    • 2019-02-11
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    相关资源
    最近更新 更多