【问题标题】:I cannot connect to the database in Visual Basic我无法连接到 Visual Basic 中的数据库
【发布时间】:2012-06-05 19:55:24
【问题描述】:

我有以下代码:

    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

【问题讨论】:

标签: database vb.net connection-string


【解决方案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 程序将无法访问它,并且只会限制对您的虚拟主机的访问。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2017-06-21
        • 1970-01-01
        • 1970-01-01
        • 2017-08-17
        • 1970-01-01
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        • 2017-09-06
        相关资源
        最近更新 更多