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
Dave Brock
  • 287
  • 3
  • 5
  • 14
Emil Dumbazu
  • 662
  • 4
  • 22
  • 37
  • @EmilDumbazu: See if this helps? http://www.sswug.org/articlesection/default.aspx?TargetID=44331 – Siddharth Rout Jun 05 '12 at 20:06
  • @SiddharthRout i'm using 2008 version , the first "hint" in that article is about 2005 version – Emil Dumbazu Jun 05 '12 at 20:11
  • @EmilDumbazu: That would give you a basic idea what could be wrong. :) What about this? http://stackoverflow.com/questions/360141/how-to-connect-to-local-instance-of-sql-server-2008-express – Siddharth Rout Jun 05 '12 at 20:15

3 Answers3

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.

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

Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47
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

Kris Khairallah
  • 1,537
  • 13
  • 16