views:

74

answers:

5

I'm using MS Visual Studio 2010 to create an application with SQL Server 2008 database access, but what I did to create the database was add a new "SQL Server 2008 Database Project", it added it, and shows me everything on my Solution Explorer, but how do I write the connection string to connect to it, because I wrote this one, and it didn't work.

SqlConnection cnTrupp = new SqlConnection("Initial Catalog = Database;Data Source = localhost;Persist Security Info=True;");

update:

I used this one:

cnTrupp = new SqlConnection("database=DB_Trupp;server=.\\SQLExpress;Persist Security Info=True;integrated security=SSPI");

But when I use the cnTrupp.Open() it tells me that the login failed.

A: 

You need to specify how you'll authenticate with the database. If you want to use integrated security (this means using Windows authentication using your local or domain Windows account), add this to the connection string:

Integrated Security = True;

If you want to use SQL Server authentication (meaning you specify a login and password rather than using a Windows account), add this:

User ID = "username"; Password = "password";
Adam Robinson
+2  A: 

Standard Security
Data Source=serverName\instanceName;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Trusted Connection
Data Source=serverName\instanceName;Initial Catalog=myDataBase;Integrated Security=SSPI;

Here's a good reference on connection strings that I keep handy: ConnectionStrings.com

JohnFx
ok, but I'm going to install it on another computer, How I'm supposed to know the other's computer name?
Osukaa
Install what? The database or the application? The only thing you care about in your connection string is where the database is.
Abe Miessler
I think you might be confused serverName\instanceName refers to the SQLServer, not the machine the code is running on.
JohnFx
A: 

Copy/Paste what is below into your code:

SqlConnection cnTrupp = new SqlConnection("Initial Catalog = Database;Data Source = localhost;Persist Security Info=True;Integrated Security = True;");

Keep in mind that this solution uses your windows account to log in.

As John and Adam have said, this has to do with how you are logging in (or not logging in). Look at the link John provided to get a better explanation.

Abe Miessler
`localhost` is a system identifier for the local adapter. It will work just fine; try it in SSMS.
Adam Robinson
@Adam Robinson: you're absolutely right - works like a charm. Not sure where I got that from.... ok, learned something new today! :-)
marc_s
+1  A: 

Check out the connection strings web site which has tons of example for your connection strings.

Basically, you need three things:

  • name of the server you want to connect to (use "." or (local) or localhost for the local machine)
  • name of the database you want to connect to
  • some way of defining the security - either integrated Windows security, or define a user name / password combo

For example, if you want to connect to your local machine and the AdventureWorks database using integrated security, use:

server=(local);database=AdventureWorks;integrated security=SSPI;

Or if you have SQL Server Express on your machine in the default installation, and you want to connect to the AdventureWorksLT2008 database, use this:

server=.\SQLExpress;database=AdventureWorksLT2008;integrated Security=SSPI;
marc_s
`localhost` has nothing to do with the web or HTTP. It will work fine with SQL Server.
Adam Robinson
Can you provide a source for the localhost vs (local) vs . info? I always thought you could use localhost...
Abe Miessler
@Abe Miessler: no, you're totally right - localhost works just as well. Sorry for the confusion.....
marc_s
A: 

Instead of writing it in your code directly I suggest you make use of the dedicated <connectionStrings> element in the .config file and retrieve it from there.

Also make use of the using statement so that after usage your connection automatically gets closed and disposed of.

A great reference for finding connection strings: connectionstrings.com/sql-server-2008.

XIII
-1 This has nothing to do with the actual question.
Adam Robinson
+1 he makes a good point and provides a resource for connection string info.
Abe Miessler
@Abe: His "point" is "look it up". Adding that to an answer that addresses the actual problem is good. Posting that with information that, while good to know, is unrelated to the question is not.
Adam Robinson
I agree that just posting a link is weak sauce. I thought he made a good point (though somewhat off topic) in saying that he should put it in the .config file. If he had just posted the link or just posted the .config suggestion i would agree with a -1 score. Together I think it deserves a 0 score.
Abe Miessler
But the link is in the answer. Just click on the <connectionStrings> part which guides the OP to the MSDN page and from there on a code snippet on how to read it out can be found. I had some problems with the markdown, still learning it, to combine [ and <
XIII