【发布时间】:2020-12-25 01:36:00
【问题描述】:
我不明白为什么我无法连接到我的 SQL Server Express LocalDB。我不断遇到连接字符串的麻烦。这是我尝试过的:
Imports System.Data.OleDb
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim conString As String 'Connection string
Dim con As OleDbConnection 'Connecting to your database
Dim Command As OleDbCommand 'Query "What do you want in the database"
'conString = "PROVIDER=System.Data.SqlClient v4.0; Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DatabaseInnlogging.mdf;Integrated Security=True"
'conString = "PROVIDER=SQLOLEDB; Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DatabaseInnlogging.mdf;Integrated Security=True"
'conString = "PROVIDER=System.Data.SqlClient; Data Source=C:\Users\Bruker\source\repos\InnloggingFørsøkv1\InnloggingFørsøkv1\DatabaseInnlogging.mdf;Integrated Security=True"
'conString = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\Bruker\source\repos\InnloggingFørsøkv1\InnloggingFørsøkv1\DatabaseInnlogging.mdf;Integrated Security=True"
'conString = "PROVIDER=System.Data.SqlClient v4.0; Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DatabaseInnlogging.mdf;Integrated Security=True"
Try
con = New OleDbConnection(conString)
con.Open()
Command = New OleDbCommand("select * from LogInTable where UserID = ? and Password = ?", con)
Dim parm1 As OleDbParameter, parm2 As OleDbParameter
parm1 = Command.Parameters.Add("@UserID", OleDbType.VarChar)
parm2 = Command.Parameters.Add("@Password", OleDbType.VarChar)
parm1.Direction = ParameterDirection.Input
parm2.Direction = ParameterDirection.Input
parm1.Value = Me.TextBox1.Text
parm2.Value = Me.TextBox2.Text
Command.Connection.Open()
Dim reader As OleDbDataReader
If reader.Read = True Then
Me.DialogResult = DialogResult.OK
Else
MsgBox("Invalid UserID or Password")
End If
reader = Command.ExecuteReader
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
我尝试了几个连接字符串,但都没有工作。
但是对于数字 4,我得到了错误
多步 OLE DB 操作产生错误。检查每个 OLE DB 状态值
其他导致错误:
提供商无法识别
我的另一个尝试是SqlClient:
Imports System.Data.SqlClient
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myConn As SqlConnection
myConn = New SqlConnection("Initial Catalog=OutComes; Data Source=localhost;Integrated Security=SSPI;")
'"Data Source = (LocalDB) \ MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DatabaseInnlogging.mdf;Integrated Security=True")
' "Data Source=localhost;Integrated Security=SSPI;")
'Create a Command object.
Dim myCmd As SqlCommand
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT UserID, Password FROM LogInTable"
'Open the connection.
myConn.Open()
Dim results As String
Dim myReader As SqlDataReader
myReader = myCmd.ExecuteReader()
'Traverse the DataSet and Display in GUi for Example:
Do While myReader.Read()
results = results & myReader.GetString(0) & vbTab &
myReader.GetString(1) & vbLf
Loop
'Display results.
MsgBox(results)
' Close the reader and the database connection.
myReader.Close()
myConn.Close()
End Sub
我的错误是:
System.Data.SqlClient.SqlException: '连接到 SQL Server 时发生与网络相关或特定于实例的错误。服务器未找到或不可用。验证实例名称是否正确以及 SQL Server 是否配置为接受外部连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)
我的 SQL Server 数据库中有一个表,其中包含两列 UserID 和 password。
如果有人能在这里指出正确的方向,我将不胜感激。我已经阅读了很多关于这个主题的帖子,但似乎无法找到我哪里出错了。我的主要目标是连接我的数据库。
【问题讨论】:
-
您确定您已实际安装 SQL Server Express LocalDB 包??您是否从 Microsoft 站点下载并运行安装?最后一个错误似乎清楚地表明您尝试连接的数据库服务器不存在(例如,未安装)。如果您已经下载并安装了一些东西 - 究竟是什么您安装了什么?
-
我安装了 sql server,但是为了更好的衡量,我按照你的意图重新安装了它。原始项目未连接,错误消息与之前相同。我上次忘记说的是,我使用表的设计视图连接到数据库没有问题。添加数据源。然后将表格拖放到表格上。这是一个手动练习,我无法开始工作。
标签: sql sql-server vb.net connection-string localdb