3

//SQL PART

Line 1 : string dd = "Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\HEX\Documents\Visual Studio 2008\Projects\cventry_address_book_0.1\cventry_address_book_0.1\addressbook.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True";

Line 2 :SqlConnection sqlconobj = new SqlConnection(dd);

Line 3: sqlconobj.Open();

---------Errors Output------------

Unexpected character'\'

1

6 Answers 6

3

In C# the backslash character has special meaning.
You need to double it or prefix your entire string with the Verbatim character @
And there is no need to put a double quote before and after the file name.
Formatting rules (the ending semicolon) allows spaces in path or file name for the AttachDbFileName

 string dd = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + 
             @"C:\Users\HEX\Documents\Visual Studio 2008\" + 
             @"Projects\cventry_address_book_0.1\cventry_address_book_0.1" + 
             @"\addressbook.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
Sign up to request clarification or add additional context in comments.

Comments

2

Try:

string dd = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\Users\HEX\Documents\Visual Studio008\Projects\cventry_address_book_0.1\cventry_address_book_0.1\addressbook.mdf"";Integrated Security=True;Connect Timeout=30;User Instance=True";

You need to escape the string by using the @ character. Alternative you could replace single \'s with \\.

4 Comments

Even if you use @, the quotes still need to be escaped using "".
@JLRishe have you tried? The ending semicolon removes the ambiguity of normal paths
@Steve The quotes may not be necessary at all; I'm not sure on that point and that's not what my comment was referring to. What I can say for sure is that Darren's answer was invalid (as in, would not compile) the way he entered it, because it had unescaped quotes midstring.
Never had the chance to test with a Sql Server db, but I can assure you that a simple Access database placed in a path with spaces don't need any double quote
1

You should escape the string by prefixing it with the @ character. Also you should wrap your SqlConnection instance in a using statement:

string dd = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\Users\HEX\Documents\Visual Studio 2008\Projects\cventry_address_book_0.1\cventry_address_book_0.1\addressbook.mdf"";Integrated Security=True;Connect Timeout=30;User Instance=True";
using (SqlConnection sqlconobj = new SqlConnection(dd))
{
    sqlconobj.Open();
}

Comments

0

You need escape the \ character. Use this:

string dd = @"Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\HEX\Documents\Visual Studio 2008\Projects\cventry_address_book_0.1\cventry_address_book_0.1\addressbook.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True";

More: String literals

Comments

0
string dd = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Users\\HEX\\Documents\\Visual Studio 2008\\Projects\\cventry_address_book_0.1\\cventry_address_book_0.1\\addressbook.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True";

Comments

0

This should be your query connection string

string dd = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + 
                            @"C:\Users\HEX\Documents\Visual Studio 2008\Projects\cventry_address_book_0.1\cventry_address_book_0.1\addressbook.mdf" 
                            + ";Integrated Security=True;Connect Timeout=30;User Instance=True";

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.