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.

