从Excel导出到SQL:用户login失败

我试图在Excel中创build一个function,通过点击一个button将数据写入到我的远程SQL Server数据库。 但是,我不断得到一个错误告诉我,我的login失败用户“用户名”(错误80040e4d)。

起初我以为用户/login在数据库中肯定有问题,所以我去了远程桌面,打开SQL Server Management Studio,检查了我所有的权限,确认了Windows身份validation,选中了密码等等。仍然没有工作。

我的用户已经被授予读写权限,所以我尝试从SQL导入数据到Excel中,这确实有效。 所以我假设这必须是我的VBA代码,更具体地说,我的连接string的问题。

如果有人可以看一看,我会很感激,并告诉我我做错了什么? 或者如果别人有这个问题,并知道是什么原因造成的?

这是我有的代码:

Sub Button3_Click() Dim conn As New ADODB.Connection Dim iRowNo As Integer Dim sArtistId, sForename, sSurname, sNationality, sBirthDate, sDeathDate As String With Sheets("NewArtist") 'Open a connection to SQL Server conn.Open "Provider=SQLOLEDB;Data Source=tcp:IPADDRESS,1433\SqlServer2008;" & _ "Initial Catalog=DatabaseName;User ID=UserName;Password=PassWord" & _ "Integrated Security=SSPI;" 'Skip the header row iRowNo = 2 'Loop until empty cell in CustomerId Do Until .Cells(iRowNo, 1) = "" sCustomerId = .Cells(iRowNo, 1) sFirstName = .Cells(iRowNo, 2) sLastName = .Cells(iRowNo, 3) 'Generate and execute sql statement to import the excel rows to SQL Server table conn.Execute "insert into dbo.Components (ArtistId, Forename, Surname, Nationality, BirthDate, DeathDate) values ('" & sArtistId & "', '" & sForename & "', '" & sSurname & "', '" & sNationality & "', '" & sBirthDate & "', '" & DeathDate & "')" iRowNo = iRowNo + 1 Loop MsgBox "Artists imported." conn.Close Set conn = Nothing End With End Sub 

非常感谢你!

TW

Integrated Security=SSPI

你的连接string中的这个参数意味着你要传递你的Windows主体名称作为身份validation,所以sqllogin和密码被忽略。

如果要使用指定的用户名和密码,则应从连接string中删除此名称/值对。