Pijush Gupta
Pijush Gupta

Reputation: 41

Connection String in C#.Net

//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'\'

Upvotes: 3

Views: 2273

Answers (6)

Steve
Steve

Reputation: 216333

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";

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1039348

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();
}

Upvotes: 1

Darren
Darren

Reputation: 70776

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 \\.

Upvotes: 2

nsconnector
nsconnector

Reputation: 836

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";

Upvotes: 0

Ivan Marinin
Ivan Marinin

Reputation: 128

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";

Upvotes: 0

Iswanto San
Iswanto San

Reputation: 18569

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

Upvotes: 0

Related Questions