0

I am using the built-in database for my program. When I try to put in the connection string, VB cannot detect the connection string and shows a syntax error on line 7 after the new SqlConnection. I am sure that I copied the complete connection string from the properties page.

I read this post but it seems to be a different question. Below is my code for the connection. Is there any mistake in my code? Thanks for all the help!

    Imports System.Data.SqlClient 
    Public Class Login
    Dim cmd As SqlCommand
    Dim dr As SqlDataReader
    Dim da As SqlDataAdapter
    Dim sql As String
    Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf";Integrated Security=True")

1 Answer 1

1

That'll obviously show you a syntax error, look at your following line:

"C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf"

Replace the double-quotes to ""<abc>"" to get like "<abc>" on execution because you've already used "<abc>" in New SqlConnection("...").

Rather than:

Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf";Integrated Security=True")

you should have:

Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf"";Integrated Security=True")
Sign up to request clarification or add additional context in comments.

2 Comments

Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf"";Integrated Security=True") When I do what you suggest the system just recognise the whole line as text.
@Sukasa, the whole line is not recognised as text, but the everything within the parentheses is recognised as a single String, which is what should be the case. You can even see it in the syntax parser on this site. In your question, the file path part of your connection string is white, because it is not recognised as part of the String. If you double up the double-quotes then the whole lot is the same colour because it is recognised as a single String literal. The same happens in VS. I have added an edit to this answer that shows just that.

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.