![](/img/trans.png)
[英]Can't access Local SQL database manually by connection string in VB.NET using Visual Studio
[英]Connection string problem connecting to local SQL database using VB.NET in Visual Studio
我不明白為什么我無法連接到我的 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 服務器時發生與網絡相關或特定於實例的錯誤。 服務器未找到或不可用。 驗證實例名稱是否正確,以及 SQL 服務器是否配置為接受外部連接。 (提供者:命名管道提供者,錯誤:40 - 無法打開與 SQL 服務器的連接)
我的 SQL 服務器數據庫中有一張表,其中包含兩列UserID
和password
。
如果有人能在這里指出正確的方向,我將不勝感激。 我已經閱讀了很多關於這個主題的帖子,但似乎找不到我 go 錯誤的地方。 我的主要目標是連接我的數據庫。
由於您是新手,您可以嘗試一些可視化工具,它可以幫助您看得更清楚,我建議您嘗試這種方法,這是您解決方案的一半:
在您的視覺工作室中:
Menu Tools
Connection to database
在對話框中
Data Source:
Microsoft SQL 服務器(sqlClient)
Server Name:
****
你應該在這里找到你的服務器,select 它,否則這意味着你的服務器安裝有問題
Authentification:
Windows 認證
正如我在您的示例中看到的那樣,您沒有使用 SQL id,所以沒關系
Connection to database:
Select 您已經創建的數據庫,如果沒有看到這意味着您沒有創建或您沒有訪問權限
然后單擊按鈕Test Connection
如果成功然后單擊OK
然后 go 到你的ToolBox
Data
DataGridView
把它放到你的表格 select 數據源:在底部你會看到+ Add the data source
點擊它
您將有一個對話框,選擇Database
-> Dataset
-> 選擇您的數據連接,您應該會在 combobox 中看到一行,其中包含your server name \ your Database.dbo
選中復選框"Show the connection string saved in the application"
你會清楚地看到你的連接字符串
下一步 -> 下一步 > 檢查表格
結束
如果您卡在某個部分,請告訴我通過編輯來澄清您
首先,謝謝你:),我讓它工作了。 但我還有一些問題。 我從一個新項目開始,並制作了一個本地 sqlDB,其中一個表有兩個列“UserID”和“Password”。 我在此表中有一行帶有“Admin”和“pass”我沒有使用表設計器或添加數據源。
我按照您對信件的描述並在工具連接下復制了我的連接字符串...效果很好,我找到了 mdf 文件並能夠連接到它
使用數據網格加載表單並將其綁定到數據源。 它工作正常
然后我嘗試手動連接到新按鈕下的數據庫。 我收到此錯誤消息:
System.Data.SqlClient.SqlException:
'Cannot attach file 'C:\Users\Bruker\source\repos\Connect_v2\Connect_v2\bin\Debug\Database1.mdf'
as database 'OutComes
' because this file is already in use for database
'C:\USERS\BRUKER\SOURCE\REPOS\CONNECT_V2\CONNECT_V2\BIN\DEBUG\DATABASE1.MDF''
This was the class:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Database1DataSet.LogInTable' table. You can move, or remove it, as needed.
Me.LogInTableTableAdapter.Fill(Me.Database1DataSet.LogInTable)
End Sub
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=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30")
'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
我試圖注釋掉加載事件並手動連接到數據庫。 有效:)
Imports System.Data.SqlClient
Public Class Form1
'Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 'TODO: This line of code loads data into the 'Database1DataSet.LogInTable' table. You can move, or remove it, as needed.
' Me.LogInTableTableAdapter.Fill(Me.Database1DataSet.LogInTable)
'End Sub
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=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30")
'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
End Class
這很好,但我仍然想知道為什么我收到錯誤消息。 是不是因為 datagridview tableadapter 擁有與數據庫的持續打開連接。 並且不可能同時有多個打開的連接?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.