Multiple flavor Connection Strings

Hi, I've been trying to access database to VS. I wrote some code shown below.

using System.Data;
using System.Data.SqlClient;

namespace ADO1
{
class Program
{
static void Main(string[] args)
{
SqlConnection cn = new SqlConnection();
try
{
cn.ConnectionString = "Integrated Security=SSPI;" + "Initial Catalog=AdventureWorks;" +
"Data Source=(local)";
cn.Open();

SqlCommand dataCommand = new SqlCommand();
dataCommand.Connection = cn;
dataCommand.CommandText = "Select Person.Contact.LastName, Person.Contact.FirstName";
dataCommand.CommandText += "From Person.Contact where LastName like 'Smith'";
Console.WriteLine("Results: {0}\n", dataCommand.CommandText);

SqlDataReader dataReader = dataCommand.ExecuteReader();
while (dataReader.Read())
{
string lastName = dataReader.GetString(0);
string firstName = dataReader.GetString(1);
Console.WriteLine("{0} {1}", lastName, firstName);
}
}
//catch (SqlException ex)
//{
// Console.WriteLine(ex.Message);
//}
finally
{
cn.Close();
Console.ReadLine();
}
}
}
}

Then, it throws SQLException with this message.

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

I believe that I should learn more about ConnectionString.

Any tips will appreciate.

Thanks.

[1761 byte] By [KentaroM] at [2007-12-25]
# 1

seems like you are using SqlExpress correct? IF so, the connection string should be this:

cn.ConnectionString = "Integrated Security=SSPI;Initial Catalog=AdventureWorks;Data Source=.\SQLExpress";

Does this work for you?

ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Problem is in connection string. You will solve it realy easy if you already have a connection from VS Server explorer because you can copy connection string from connection object properties. We can only gues now what is the problem in connection string. Here is my gues that will be added to prevoius suggestion. If you use SQL Express then you need parameter AttachDBName. Maybe Integrated Security should be True instead of SSPI if you use SQL 2005 instead of SQL 2000. But this is all guesses, so you can solve the problem from your current established connection.
boban.s at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
Here is a site ConnectionStrings.Com which gives examples of connection strings to different types of databases as well as different ways to connect.
OmegaMan at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 4

\SQLExpress is the key. After I tried with this code, I could successfully connect to SQL Server. Thank you so much for help!

KentaroM at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...