在 vb.net 中连接到本地 sql 服务器 - 连接错误以及本地和商业之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14691505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting to local sql server in vb.net - Error Connecting and difference between Local and Commercial?
提问by Blunderfest
I am a developer building websites in VB.Net using Visual Studio 2010. I am trying to populate some fields when a user visits a website based on their decisions on a previous screen.
我是使用 Visual Studio 2010 在 VB.Net 中构建网站的开发人员。当用户根据他们在前一个屏幕上的决定访问网站时,我试图填充一些字段。
My connection information is as follows:
我的连接信息如下:
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
The error I continue to get is:
我继续得到的错误是:
Cannot open database "PREP" by the login. The login failed. Login failed for user 'OR-AA-Me\Me'
无法通过登录打开数据库“PREP”。登录失败。用户“OR-AA-Me\Me”登录失败
I assumed that since it was a local database I would not need a username and password. Am I wrong?
我假设因为它是一个本地数据库,所以我不需要用户名和密码。我错了吗?
Also, are there glaring practices in the above code that will be unwise when I transport this to a commercial environment?
另外,当我将其传输到商业环境时,上述代码中是否存在不明智的明显做法?
Thanks!
谢谢!
采纳答案by Blunderfest
Solved:
解决了:
The connection string simply needed additional filepath information.
连接字符串只需要额外的文件路径信息。
The successful connection information was:
连接成功的信息是:
myConnection = New SqlConnection("Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\PREP.mdf; Integrated Security=True; User Instance=True")
myConnection = New SqlConnection("Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\PREP.mdf; Integrated Security=True; User Instance=True")
There was no need for a username as the Integrated Security set to True assumes that the computer will use the login information for the user on the actual computer he is using.
不需要用户名,因为集成安全设置为 True 假定计算机将使用用户在他正在使用的实际计算机上的登录信息。

