0

编辑: SQL 连接已更新。

控制器:

Imports System.Data.SqlClient   

Function SQLSelect() As ActionResult

    Dim theconnection As New SqlConnection("Data Source=MSSQLSERVER;server=(localdb)\Projects;Database=test_drive_database;User Id=xxxx_user-PC\xxxx_user;password=;Trusted_Connection=True;Integrated Security=True;")
    theconnection.Open()

    Dim queryString As String = "SELECT * FROM ColorTable"
    Dim command As New SqlCommand(queryString)
    command.BeginExecuteNonQuery()

    command.CommandTimeout = 15
    command.CommandType = CommandType.Text

    'Printing Out the SQL Result

    Return ViewData("command")

End Function

错误消息:

Cannot open database "test_drive_database" requested by the login. The login failed.

Login failed for user 'xxxx_user-PC\xxxx_user'.

如何找出登录失败的确切原因?

注意: 不使用密码。

附录:

        Dim theconnection As New SqlConnection("Data Source=MSSQLSERVER;server=(localdb)\Projects;Database=ColorTable_database.sdf;Integrated Security=sspi;")
    theconnection.Open()

这也是我尝试过的一种变体,尽管我收到了相同的错误消息。

4

2 回答 2

2

参考SQL Server 的连接字符串 按以下方式更改连接字符串:

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

如果您想使用默认登录名(窗口登录名)登录,请使用以下受信任连接:

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

所以你的连接对象应该是:

Dim theconnection As New SqlConnection("server=(localdb)\Projects; _
                          Database=test_drive_database;Trusted_Connection=True;")

如果这不能解决问题,则可能是用户的权限问题。检查参考链接:

SQL Server Asp.Net - “登录失败”
SQL Server 用户通过 asp.net 页面
登录失败 SQL Server:用户
登录失败 用户登录失败

于 2012-11-21T08:22:50.887 回答
0

尝试添加“Trusted_Connection = true;” 在连接字符串上。

于 2012-11-21T08:24:45.070 回答