0

I have the following code:

    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

As you can see i would like to open a connection to the database but every time I want test it I get this error:

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)

I struggled for more than 2 hours, so please help me!

Later edit:

I've tried this:

 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

but it throw me this error:

   Unrecognized database format
3

3 Answers 3

1

Here is an excerpt of the same code I use to connect to my databases:

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

Try that.

Be sure to add the resource MySQL.Data into your project and also call it using:

Imports MySQL.Data.MySQLClient

ALSO! Be certain that when you created the database that you enabled external database access. If you don't do that then VB programs will not be able to access it and it will limit access to your webhost only.

Sign up to request clarification or add additional context in comments.

Comments

0

The Jet OLEDB Provider is for Access databases. It can't handle sdf files. Try with the correct connection string.

You can take help of this website to get the correct connection string for your database: www.ConnectionStrings.com

Comments

0

Is the application built on shared storage and you are trying to run it from local machine? That could be an issue if the server containing the app does not have access to db

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.