连接到vb.net中的本地sql server - 错误连接和本地和商业之间的区别?

时间:2013-02-04 16:56:15

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

我是使用Visual Studio 2010在VB.Net中构建网站的开发人员。当用户根据他们在之前屏幕上的决定访问网站时,我试图填充一些字段。

我的连接信息如下:

Dim myDataReader As SqlDataReader
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim strSQL As String

myConnection = New SqlConnection("Data Source=localhost\sqlexpress; Database=PREP; Integrated Security=SSPI;")
Try
    myConnection.Open()
Catch ex As Exception
    MsgBox(ex.Message)
End Try

strSQL = "SELECT * FROM Charts where ID=1"

myCommand = New SqlCommand(strSQL, myConnection)

myDataReader = myCommand.ExecuteReader()

If myDataReader.Read() Then
    TextBox1.Text = myDataReader.Item("Priority1")
Else
    MsgBox("Didn't work...")
End If

我继续得到的错误是:

  

无法通过登录打开数据库“PREP”。登录失败。用户'OR-AA-Me \ Me'

登录失败

我认为因为它是本地数据库所以我不需要用户名和密码。我错了吗?

此外,上述代码中的明显做法在我将其传输到商业环境时是不明智的吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

解决:

连接字符串只需要额外的文件路径信息。

成功的连接信息是:

  

myConnection = New SqlConnection(“Data Source =。\ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ PREP.mdf; Integrated Security = True; User Instance = True”)

不需要用户名,因为Integrated Security设置为True假定计算机将使用他正在使用的实际计算机上的用户的登录信息。


相关问题