views:

41

answers:

2

I am trying to learn ADO.NET to connect with SQL Server ... I install SQL Server with Visual Studio .... there is data base called "Northwind" as example with it ... I am trying to read this database , I wrote this code ...

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

namespace DataSetReaderFromSQL
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection connection = new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=Northwind");
            connection.Open();
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "Select CustomerID , CompanyName from Customers";
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
                Console.WriteLine(reader["CustomerID"] +""+ reader["CompanyName"]);
            reader.Close();
            connection.Close(); 
        }
    }
}

When the application runs, it's take a little of time then throw exception when it trys to open connection ... the text of the exception :

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

I am using Windows 7 as my operating system, and I put username on my account ... I am sure that SQL Server was installed on my computer where is my error ??

+3  A: 

Visual Studio includes Express edition of SQL Server. Thus, your connection string should most likely look like "Data Source=(local)\sqlexpress;...".

Anton Gogolev
+3  A: 

Either

  • You haven't configured Sql Server to allow remote connections, as the error message tells you =)
  • Your datasource is wrong. Try .\SqlExpress or just . Ordinarily when Visual Studio installs Sql Server Express, it installs it as a named instance called SqlExpress.
Rob
thanks ... yes I wasn't configured Sql Server ... when I do that and put sqlexpress in Data source text .... a new exception is thrown ... ((cannot open data set "MyDataBase" request by the login . The login failed . Login failed for user "Farah-PC/Farah" )) .
Farah_online