【发布时间】: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”。关于如何解决此问题的任何想法?
谢谢你, 阿西夫
【问题讨论】:
-
这是实际代码还是你匿名]连接字符串
-
不知道这是否是个问题,但至少在我看来很奇怪:为什么
<DOMAIN> \ axzahir的反斜杠周围有空格? -
@SimonPrice 嘿,我已经匿名了连接字符串。我只留下了我的用户名。
-
我认为 Integrate Security 可能会忽略您的用户名/密码。
-
无关提示:
SqlConnection、SqlCommand和SqlDataReader都是IDisposable,所以应该在using块中。并使用参数去除SQL注入漏洞。
标签: sql sql-server vb.net visual-studio