Connection string to SQL Server 2012 "LocalDB"

Connection string to SQL Server 2012 "LocalDB"

April 2, 2014
Hello,

Could I get a little help on my very first attempt to write an app with code that connects to my own database table on my own computer. Using Windows8.1, Visual Studio 2013 Express bundled with SQL Server2012 Express localDB.

I am trying to use code to connect with a database file that I just created on localDB (as above). Generally, I am trying to write an app to connect to anyone's localDB, but I am testing it by having it open a database connection on my own localDB on my own PC first, naturally.

I am using the following attempt inspired by this website's direction on the matter:

"Server=(LocalDB)\v11.0;Integrated Security=true;AttachDbFileName=C:\Users
\John\Documents\Visual Studio2013\Projects
\ADONET2ConnectedExpress\Database1.mdf"

and I am using all of that above within the parentheses() of:

SqlConnection myConnection = new SqlConnection();

But it doesn't work. Any advice would be greatly appreciated?
Thanks

John

1 answer

Wow. All I had to do was put an extra backslash in the "Data Source=(localDB)\v11.0; etc." like I just did.

The full line of code is:

SqlConnection myConnection = new SqlConnection("Data Source(LocalDB)\v11.0;
AttachDbFileName=C:\Users\John\Documents\Visual Studio 2013\Projects\
ADONET2ConnectedExpress\ADONET2ConnectedExpress\Database1.mdf;
Integrated Security=True");
...And this seems to work fine.

Silly oversight on my part. But now we know that a person can use C# code to:

1: Make her own data source, in my case(a him), a simple 'Customers' table in VS 2013 Express,
bundled with SQL Server2012 Express LocalDB,

2: Write Code in C# in the code window of the very same project on the IDE(design environment),
without closing the database on which I made the Customers table, and in that code, write a
connection string that will connect to, command, and retrieve data from that Customers table,
again, on a localDB.

Please note: I take no credit for myself for designing, imagining, or even innovating any of the above. In all of the above activity, I am following and taking direction from lessons of a particular online tutorial series.

John