login about faq

To prove you're not a spammer, email newuser.lgqa@gmail.com with the subject "Account Request" to request an account.

I am currently trying to connect to a database with visual basic and it keeps bring up an error saying it cant connect her is the connection code

Public Sub query()
    Dim connetionString As String
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim adapter As New SqlDataAdapter
    Dim ds As New DataSet
    Dim i As Integer
    Dim sql As String

    connetionString = "Data Source=|DataDirectory|\recipes.sdf;Password=qwert1234;Persist Security Info=True"
    sql = "SELCT COUNT(id) from locationtable"

    connection = New SqlConnection(connectionString)
        command = New SqlCommand(sql, connection)
        adapter.SelectCommand = command

        For i = 0 To ds.Tables(0).Rows.Count - 1
            MsgBox(ds.Tables(0).Rows(i).Item(0) & "  --  " & ds.Tables(0).Rows(i).Item(1))

    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try
End Sub

asked Jun 21 '10 at 18:43

opensdwsystems's gravatar image


Is it possible to get this SDF file into an actual database server? Most connectionstrings look like this:

Data Source=ServerName;Initial Catalog=DatabaseName;User ID=userid;Password=password

If not, try playing around with your Data Providers, which should be a property of your connection object.

Also, is |DataDirectory| a literal string or are you substituting that for where your SDF file is actually located?

answered Jun 22 '10 at 04:58

tsilb's gravatar image


edited Jun 22 '10 at 05:00

SDF files are files created by SQL Server. I suggest working out how to import that file into SQL server, and changing your Source= to the servername as suggested by tsilb.

answered Jul 14 '10 at 15:23

Seb's gravatar image


Just a detail is the string variable was declared as "connetionString" (with no "c"), and it was used as "SqlConnection(connectionString)", with "c".

answered Mar 07 '11 at 10:02

hugot101's gravatar image


Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Join Us in the Chat Room



Asked: Jun 21 '10 at 18:43

Seen: 6,807 times

Last updated: Mar 07 '11 at 10:02