【问题标题】:Why is Visual Studio (VB) not reading my SQL connection string properly?为什么 Visual Studio (VB) 不能正确读取我的 SQL 连接字符串?
【发布时间】:2018-11-26 18:39:45
【问题描述】:

我正在编写一个小型 Windows 工具来搜索几个 SQL 数据库。我能够毫无问题地连接和搜索第一个数据库,但是当我尝试搜索第二个数据库(数据库 2)时,我不断收到以下登录错误:

'System.Data.SqlClient.SqlException'发生在 System.Data.dll 用户“\azahir”登录失败

您会发现<Domain>\azahir 甚至没有在我的连接字符串或我的几行代码中的任何地方指定。

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1

Dim Conn As SqlConnection
Dim Conn2 As SqlConnection

Private Sub btSearch_Click(sender As Object, e As EventArgs) Handles btSearch.Click
    Conn = New SqlConnection("Data Source = <SERVER>;Initial Catalog=<DATABASE>;Integrated Security=SSPI;User ID = <Domain> \ axzahir;Password=<Password>;")
    Conn.Open()

    Dim cmd2 As SqlCommand = Conn.CreateCommand
    cmd2.CommandText = "select firstname, lastname
    from systemuserbase where firstname like" + "'%" + TxFirstName.Text + "%'" +
    " And lastname Like" + " '%" + TxLastname.Text + "%'"

    Dim dir As SqlDataReader = cmd2.ExecuteReader()
    If dir.HasRows Then

        Dim dtClient As New DataTable
        dtClient.Load(dir)
        dtOutput.DataSource = dtClient

    End If

    dir.Close()
    Conn.Close()
End Sub

....

Private Sub btnArgus_Click(sender As Object, e As EventArgs) Handles btnArgus.Click
    Conn2 = New SqlConnection("Data Source = <SERVER2>;Initial Catalog=<DATABASE 2>;Integrated Security=SSPI;User ID = <DOMAIN> \ axzahir;Password=<PASSWORD>;")
    Conn2.Open()

    Dim cmd3 As SqlCommand = Conn2.CreateCommand
    cmd3.CommandText = "select userID, Fullname
    from Users where FullName like" + "'%" + TxFirstName.Text + "%'" +
    " And Fullname Like" + " '%" + TxLastname.Text + "%'"

    Dim dir3 As SqlDataReader = cmd3.ExecuteReader()
    If dir3.HasRows Then

        Dim dtClient As New DataTable
        dtClient.Load(dir3)
        dtOutput.DataSource = dtClient


    End If

    dir3.Close()
    Conn2.Close()
End Sub
End Class

我已验证我的域/用户名 + 密码适用于数据库 2。我很难理解为什么 Visual Studio 认为我的用户是“\azahir”而不是指定的“\axzahir”。关于如何解决此问题的任何想法?

谢谢你, 阿西夫

【问题讨论】:

  • 这是实际代码还是你匿名]连接字符串
  • 不知道这是否是个问题,但至少在我看来很奇怪:为什么&lt;DOMAIN&gt; \ axzahir 的反斜杠周围有空格?
  • @SimonPrice 嘿,我已经匿名了连接字符串。我只留下了我的用户名。
  • 我认为 Integrate Security 可能会忽略您的用户名/密码。
  • 无关提示:SqlConnection、SqlCommand 和 SqlDataReader 都是 IDisposable,所以应该在 using 块中。并使用参数去除SQL注入漏洞。

标签: sql sql-server vb.net visual-studio


【解决方案1】:

集成安全并非如此。使用集成安全性时,无法指定特定的用户名或密码。相反,您将获得运行您程序的任何用户帐户的用户授权。整个连接字符串如下所示,没有具体的用户信息:

数据源 = ;初始目录=;集成安全性=SSPI;

如果您想指定用户名和密码,您必须使用 SQL 身份验证。如果您想以特定域帐户的身份访问数据库,则使用集成安全性,但您必须以该用户身份运行您的应用。无法在连接字符串中指定 Active Directory 凭据并获取该用户的数据库访问权限。


在这里,让我向您展示一种更好的数据库连接模式。 (不会容易受到 sql 注入攻击!即使抛出异常也会记得关闭连接。)

假设一个有效的连接字符串:

Private ConnString As String = "connection string here"

Private Sub btSearch_Click(sender As Object, e As EventArgs) Handles btSearch.Click
    Dim SQL As String = _ 
      "SELECT firstname, lastname " & 
      "FROM systemuserbase " & 
      "WHERE firstname like '%' + @FirstName + '%' AND lastname Like '%' + @LastName + '%';"

    Using Conn As New SqlConnection(ConnString), _
          cmd As New SqlCommand(SQL, Conn)

        'Use actual database column types and lengths here
        cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 20).Value = TxFirstName.Text
        cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 20).Value = TxLastName.Text

        Conn.Open()   
        Using dir As SqlDataReader = cmd2.ExecuteReader()
            dtOutput.DataSource = dir
            dir.Close()
        End Using
    End Using
End Sub

Private Sub btnArgus_Click(sender As Object, e As EventArgs) Handles btnArgus.Click
    Dim SQL As String = _ 
      "SELECT userID, Fullname " & 
      "FROM Users " &  
      "WHERE FullName like '%' + @FirstName + '%' AND Fullname Like '%' + @Lastname + '%';"

    'Note I can use the same variable names. 
    ' These are scoped to the method, not the class.
    ' Different scope, different variables, even though the names are the same
    Using Conn AS New SqlConnection(ConnString), _
          cmd As New SqlCommand(SQL, Conn)

        'Use actual database column types and lengths here
        cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 20).Value = TxFirstName.Text
        cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 20).Value = TxLastName.Text

        Conn.Open()    
        Using dir As SqlDataReader = cmd.ExecuteReader()
            dtOutput.DataSource = dir 
            dir.Close()
        End Using
    End Using
End Sub

【讨论】:

    猜你喜欢
    • 2013-09-20
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多