SQL Server Developer Center > SQL Server Forums > SQL Server Data Access > SQL Database Connectivity using C Sharp
Ask a questionAsk a question
 

AnswerSQL Database Connectivity using C Sharp

  • Saturday, November 07, 2009 9:40 AMat0z Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I am using sqlserver 2005 as backend and Visual Studio 2008 as fontend in my application.
    In the course of that, I need to do get some data from database( say employee table). I am using this code :

    static

    void Main(string[] args)
    {
    SqlConnection conn = new SqlConnection("Data Source = testserver;" + "Initial Catalog = employee;");
    conn.Open();
    SqlCommand command = conn.CreateCommand();
    command.CommandText =
    "select * from Employee_Details";
    SqlDataReader reader = command.ExecuteReader();
    //display the result
    while (reader.Read())
    {
    String output = reader["Employee name"].ToString();
    Console.WriteLine(output);
    }
    reader.Close();
    conn.Close();
    }


    but getting some problem :
    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 am using windows authentication for sqlserver connection
    and my Firewall service is not running.


    I already chaeked, both named pipes & TCP/IP are enabled
    SQL browser is running
    Remote connection is also enabled.


    But, may be I am missing somthing in the code...please help me...!

Answers

All Replies