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) Try connection.Open() command = New SqlCommand(sql, connection) adapter.SelectCommand = command adapter.Fill(ds) adapter.Dispose() command.Dispose() connection.Close() 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)) Next Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub
Answer by tsilb · Jun 22, 2010 at 05:58 AM
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?
SQL and Excel? 1 Answer
Which server is better SQL or asp.net? 3 Answers
MySQL database help 0 Answers