none
connecting to sql database c# RRS feed

  • Question

  • im using visual studios 2008 C#.
    i have written this line of code at load page just to see if i can connect to a database in my local machine.
    the name of the database is "Database"
    i have named pipes and TCP/IP enabled in sql configuration manager i have opened a port which no other service is using and still i just cant seem to connect. this is my first time trying to connect to a database.
    here's the code.

    SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Database;Integrated Security=SSPI");
    conn.Open();

    Error!

    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 have tried for hour to fix this but Sad.. i just want to simply connect to a database and grab some values from it.
    any suggestions would be useful
    thanks
    Thursday, April 3, 2008 11:43 PM

Answers

  •  un33k wrote:
    im using visual studios 2008 C#.
    i have written this line of code at load page just to see if i can connect to a database in my local machine.

    i have tried for hour to fix this but .. i just want to simply connect to a database and grab some values from it.
    any suggestions would be useful
    thanks

     

    The first suggestion is not to use named pipes. I don't think anybody is using them, especially locally. Secondly, close your TCP/IP connection. You don't need it locally either.

     

    You connect to Sql server via a connection string. the server must be configured properly. You can actually set up the connection including the connection string entirely in the designer of VS2008 and Sql Server management studio. You can then copy the connection string and use it programmatically in your code. There is also a chance (I've never done it) that the connection may be referred as an object from C#.

     

    It is also important to know what your OS is: Vista or XP. You will face a different set of hurdles in Vista vs XP.

     

    Forget about the code for a while. Make sure you see your databases and tables in VS2008. They will be in the server explorer in the left pane (by default). Make sure you can see your tables and OPEN them and see the fields. You should see the values.

     

    In Vista you will have to go thru a maze of permissions, elevations and many other things.

    Friday, April 4, 2008 12:22 AM

All replies

  •  un33k wrote:
    im using visual studios 2008 C#.
    i have written this line of code at load page just to see if i can connect to a database in my local machine.

    i have tried for hour to fix this but .. i just want to simply connect to a database and grab some values from it.
    any suggestions would be useful
    thanks

     

    The first suggestion is not to use named pipes. I don't think anybody is using them, especially locally. Secondly, close your TCP/IP connection. You don't need it locally either.

     

    You connect to Sql server via a connection string. the server must be configured properly. You can actually set up the connection including the connection string entirely in the designer of VS2008 and Sql Server management studio. You can then copy the connection string and use it programmatically in your code. There is also a chance (I've never done it) that the connection may be referred as an object from C#.

     

    It is also important to know what your OS is: Vista or XP. You will face a different set of hurdles in Vista vs XP.

     

    Forget about the code for a while. Make sure you see your databases and tables in VS2008. They will be in the server explorer in the left pane (by default). Make sure you can see your tables and OPEN them and see the fields. You should see the values.

     

    In Vista you will have to go thru a maze of permissions, elevations and many other things.

    Friday, April 4, 2008 12:22 AM
  • Are you using sqlexpress? If so the database will probably a named instance. You will need to modify your connction string to account for this:

     

     

    Code Snippet

    SqlConnection conn = new SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Database;Integrated Security=SSPI");

     

     

    Hope it helps.
    Friday, April 4, 2008 2:21 PM
  •  imran.a wrote:

    Are you using sqlexpress? If so the database will probably a named instance. You will need to modify your connction string to account for this:

     

     

    Code Snippet

    SqlConnection conn = new SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Database;Integrated Security=SSPI");

     

     

    Hope it helps.

     

    At first read that string should probably work.  Check this link if it does not.

     

    http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=3110232&SiteID=1

     

    Hope this helps.  Rudedog

     

     

    Friday, April 4, 2008 3:26 PM
  • Did you add the server to the Server Explorer on Visual Studio?

    When you added, did it ask for user id and pwd?

     

     

    Friday, April 4, 2008 7:58 PM
  • Check this article ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html ) for details on how to connect to SQL Server database from C#.NET database applications as well as Java database applications. It also describes how to pass embedded SQL queries (SELECT, INSERT, UPDATE, DELETE), calling stored procedures, pass parameters etc.

    Shahriar Nour Khondokar: http://www.shahriarnk.com/
    Tuesday, August 3, 2010 12:02 PM