2012-06-05 55 views
0

我有以下代碼:我無法連接到數據庫在Visual Basic中

Dim string_conectare As String = "Data Source=|DataDirectory|\Database1.sdf" 
    Dim conexiune As SqlConnection 
    conexiune = New SqlConnection(string_conectare) 
    conexiune.Open() 

    If conexiune.State = ConnectionState.Open Then 
     MsgBox("OK") 
    Else 
     MsgBox("not ok") 
    End If 

正如你可以看到我想開到我要測試它,我得到這個錯誤的數據庫,但每一次的連接:

A network-related or instance-specific error occurred while establishing a 
connection to SQL Server. The server was not found or was not accessible. 
Verify that the instance name is correct and that SQL Server is 
configured to allow remote connections. (provider: SQL Network Interfaces, 
error: 26 - Error Locating Server/Instance Specified) 

我掙扎了2個多小時,所以請幫幫我!

後來編輯:

我已經試過這樣:

Dim string_conectare As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database1.sdf;Persist Security Info=True" 
    Dim conexiune As OleDbConnection 
    conexiune = New OleDbConnection(string_conectare) 
    conexiune.Open() 

    If conexiune.State = ConnectionState.Open Then 
     MsgBox("OK") 
    Else 
     MsgBox("not ok") 
    End If 

,但它把我這個錯誤:

Unrecognized database format 
+0

@EmilDumbazu:看看這是否有幫助? http://www.sswug.org/articlesection/default.aspx?TargetID=44331 –

+0

@SiddharthRout我使用2008版本,該文章中的第一個「提示」是關於2005版本 –

+0

@EmilDumbazu:這會給你一個基本的想法是什麼可能是錯的。 :) 那這個呢? http://stackoverflow.com/questions/360141/how-to-connect-to-local-instance-of-sql-server-2008-express –

回答

1

這裏是我用來連接相同的代碼的摘錄到我的數據庫:

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click 
    Dim conn As MySqlConnection 

    'Connect to the database using these credentials 
    conn = New MySqlConnection 
    conn.ConnectionString = "server=your server site (generally long url); user id=login id for mysql user; password=self explanatory; database=name of the DB you're trying to reach" 

    'Try and connect (conn.open) 
    Try 
     conn.Open() 

    Catch myerror As MySqlException 'If it fails do this... (i.e. no internet connection, etc.) 
     MsgBox("Error connecting to database. Check your internet connection.", MsgBoxStyle.Critical) 
    End Try 


    'MySQL query (where to call for information) 
    Dim myAdapter As New MySqlDataAdapter 

    'Tell where to find the file with the emails/passes stored 
    Dim sqlquery = "SELECT * FROM the database you selected above WHERE Email = '" & txtEmail.Text & "' AND Password = '" & txtPassword.Text & "'" 
    Dim myCommand As New MySqlCommand 
    myCommand.Connection = conn 
    myCommand.CommandText = sqlquery 

    'Start query 
    myAdapter.SelectCommand = myCommand 
    Dim myData As MySqlDataReader 
    myData = myCommand.ExecuteReader 

    If myData.HasRows = 0 Then 
     MsgBox("Invalid email address or password.", MsgBoxStyle.Critical) 

    Else 
     MsgBox("Logged in as " & txtEmail.Text & ".", MsgBoxStyle.Information) 

     Me.Close() 

    End If 

End Sub 

試試看。

確保將資源MySQL.Data添加到您的項目,並使用叫它:

Imports MySQL.Data.MySQLClient 

也!確保在創建啓用外部數據庫訪問的數據庫時。如果你不這樣做,那麼VB程序將無法訪問它,它只會限制對你的虛擬主機的訪問。

0

Jet OLEDB Provider適用於Access數據庫。它無法處理sdf文件。嘗試使用正確的連接字符串。

你可以把這個網站的幫助,以獲得正確的連接字符串爲您的數據庫: www.ConnectionStrings.com

0

是建立在共享存儲的應用程序,你正試圖從本機上運行? 這可能是一個問題,如果包含該應用程序的服務器無法訪問db