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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. DB Connection Failure using C#

DB Connection Failure using C#

Scheduled Pinned Locked Moved Database
databasequestioncsharpsql-serversysadmin
3 Posts 2 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.
  • C Offline
    C Offline
    Cadence2 0
    wrote on last edited by
    #1

    Hey there. I just installed SQL Server express 2005 yesterday and I'm trying to connect to it using C# on Win 2000 SP4. I've never used SQL server so this is probably a novice question. I used the following connection string to connect SqlConnection conn = new SqlConnection("Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs"); However, I get the following error: Exception Occured -->> System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2 005, 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 running the SQL Server service. I see the sqlservr.exe in the taskbar but I'm not sure if it is actually running. Anyone know how I can connect to the sql server from C#? Thank you! -K

    G 1 Reply Last reply
    0
    • C Cadence2 0

      Hey there. I just installed SQL Server express 2005 yesterday and I'm trying to connect to it using C# on Win 2000 SP4. I've never used SQL server so this is probably a novice question. I used the following connection string to connect SqlConnection conn = new SqlConnection("Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs"); However, I get the following error: Exception Occured -->> System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2 005, 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 running the SQL Server service. I see the sqlservr.exe in the taskbar but I'm not sure if it is actually running. Anyone know how I can connect to the sql server from C#? Thank you! -K

      G Offline
      G Offline
      god_bless_the_world
      wrote on last edited by
      #2

      On SQL Server Express, the name of the datasource is like "SERVERNAME\SQLEXPRESS" where SERVERNAME is replaced by the name of your server. Rather than localhost, may be you can try the above syntax and check if it works. In faith there is enough light for those who want to believe and enough shadows to blind those who don't. - Blaise Pascal

      C 1 Reply Last reply
      0
      • G god_bless_the_world

        On SQL Server Express, the name of the datasource is like "SERVERNAME\SQLEXPRESS" where SERVERNAME is replaced by the name of your server. Rather than localhost, may be you can try the above syntax and check if it works. In faith there is enough light for those who want to believe and enough shadows to blind those who don't. - Blaise Pascal

        C Offline
        C Offline
        Cadence2 0
        wrote on last edited by
        #3

        Indeed! Thanks for your help god_bless. Here is my final connection string: SqlConnection conn = new SqlConnection(@"Server=.\SQLEXPRESS; Connect Timeout=5; Database=myTestDb; User ID=****; Password=****"); Also, since I am not using Windows authentication, I had add a user which I accomplished by SQLCMD.exe -S (local)\SQLExpress -E > EXEC sp_adduser '[machineName]\[userName]', '[userNameInDB]' > GO > EXEC sp_ addsrvrolemember '[userName]', 'sysadmin' > GO These steps are from this (excellent) tutorial: http://www.aspfree.com/c/a/MS-SQL-Server/Configuring-SQL-Server-Express-2005/ Finally, I made sure that the server was listening for the user I specified instead of some other user; that got rid of my Login Failed attempts. Then, it finally worked!:cool: -K

        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