0

我有以下代码:

    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
4

3 回答 3

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 程序将无法访问它,它只会限制对你的虚拟主机的访问。

于 2012-06-05T22:51:21.620 回答
0

Jet OLEDB Provider 用于 Access 数据库。它无法处理 sdf 文件。尝试使用正确的连接字符串。

您可以借助该网站为您的数据库获取正确的连接字符串: www.ConnectionStrings.com

于 2012-06-06T19:04:09.140 回答
0

应用程序是否构建在共享存储上,您正尝试从本地计算机运行它?如果包含应用程序的服务器无权访问 db,这可能是一个问题

于 2015-04-16T09:27:13.163 回答