Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Problem while accessing the sqlserver2005 in c#

Problem while accessing the sqlserver2005 in c#

Scheduled Pinned Locked Moved Database
databasehelpcsharpsql-serversysadmin
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mohanallam
    wrote on last edited by
    #1

    Hi friends I am facing a problem while accessing database sqlserver 2005 from command prompt. Here i am giving my code. I don't have any compilation errors, But runtime errors as follows.... Unhandled Exception: System.Data.SqlClient.SqlException: An error has occurred w hile establishing a connection to the server. When connecting to SQL Server 200 5, this failure may be caused by the fact that under the default settings SQL Se rver does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception my code is : using System; using System.Data; using System.Data.SqlClient; /// <summary> /// Demonstrates how to work with SqlConnection objects /// </summary> class SqlConnectionDemo { static void Main() { // 1. Instantiate the connection string strConnection = "server=localhost; Trusted_Connection=yes; timeout=120"; SqlConnection Conn = new SqlConnection(strConnection); SqlDataReader rdr = null; try { // 2. Open the connection if (Conn.State == ConnectionState.Closed) { Conn.Open(); } // 3. Pass the connection to a command object SqlCommand cmd = new SqlCommand("select * from Customers", Conn); // 4. Use the connection // get query results rdr = cmd.ExecuteReader(); // print the CustomerID of each record while (rdr.Read()) { Console.WriteLine(rdr[0]); } } finally { // close the reader if (rdr != null) { rdr.Close(); } // 5. Close the connection if (Conn != null) { Conn.Close(); } } } } Note: I am executing my program at cmd prompt. Help please...........

    P S 2 Replies Last reply
    0
    • M mohanallam

      Hi friends I am facing a problem while accessing database sqlserver 2005 from command prompt. Here i am giving my code. I don't have any compilation errors, But runtime errors as follows.... Unhandled Exception: System.Data.SqlClient.SqlException: An error has occurred w hile establishing a connection to the server. When connecting to SQL Server 200 5, this failure may be caused by the fact that under the default settings SQL Se rver does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception my code is : using System; using System.Data; using System.Data.SqlClient; /// <summary> /// Demonstrates how to work with SqlConnection objects /// </summary> class SqlConnectionDemo { static void Main() { // 1. Instantiate the connection string strConnection = "server=localhost; Trusted_Connection=yes; timeout=120"; SqlConnection Conn = new SqlConnection(strConnection); SqlDataReader rdr = null; try { // 2. Open the connection if (Conn.State == ConnectionState.Closed) { Conn.Open(); } // 3. Pass the connection to a command object SqlCommand cmd = new SqlCommand("select * from Customers", Conn); // 4. Use the connection // get query results rdr = cmd.ExecuteReader(); // print the CustomerID of each record while (rdr.Read()) { Console.WriteLine(rdr[0]); } } finally { // close the reader if (rdr != null) { rdr.Close(); } // 5. Close the connection if (Conn != null) { Conn.Close(); } } } } Note: I am executing my program at cmd prompt. Help please...........

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      mohanallam wrote:

      string strConnection = "server=localhost; Trusted_Connection=yes; timeout=120";

      You haven't specified the database you want to connect to in your connection string. Visit ConnectionStrings.com[^] to see how to include this information in your connection string.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      1 Reply Last reply
      0
      • M mohanallam

        Hi friends I am facing a problem while accessing database sqlserver 2005 from command prompt. Here i am giving my code. I don't have any compilation errors, But runtime errors as follows.... Unhandled Exception: System.Data.SqlClient.SqlException: An error has occurred w hile establishing a connection to the server. When connecting to SQL Server 200 5, this failure may be caused by the fact that under the default settings SQL Se rver does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception my code is : using System; using System.Data; using System.Data.SqlClient; /// <summary> /// Demonstrates how to work with SqlConnection objects /// </summary> class SqlConnectionDemo { static void Main() { // 1. Instantiate the connection string strConnection = "server=localhost; Trusted_Connection=yes; timeout=120"; SqlConnection Conn = new SqlConnection(strConnection); SqlDataReader rdr = null; try { // 2. Open the connection if (Conn.State == ConnectionState.Closed) { Conn.Open(); } // 3. Pass the connection to a command object SqlCommand cmd = new SqlCommand("select * from Customers", Conn); // 4. Use the connection // get query results rdr = cmd.ExecuteReader(); // print the CustomerID of each record while (rdr.Read()) { Console.WriteLine(rdr[0]); } } finally { // close the reader if (rdr != null) { rdr.Close(); } // 5. Close the connection if (Conn != null) { Conn.Close(); } } } } Note: I am executing my program at cmd prompt. Help please...........

        S Offline
        S Offline
        suyuan1984
        wrote on last edited by
        #3

        you should specify the database ,user ,password in you contention string

        my english is very bad!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups