0

我是一名使用 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”登录失败

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

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

谢谢!

4

1 回答 1

0

解决了:

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

成功连接信息为:

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

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


于 2013-02-05T15:02:52.500 回答