connect to SQL server from c# program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eranby
    New Member
    • Aug 2007
    • 17

    connect to SQL server from c# program

    Hi,
    I want to add SQL server to my c# project ,I'm working on visual studio 2005
    when i added the server i could not connect it.
    can some one help me with it ?
    thanks
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    check your connection string?
    All connection strings in one place. Find the syntax for your database connection using ADO.NET, ADO, ODBC, OLEDB, C#, VB, VB.NET, ASP.NET and more.


    Is the server configured to allow connections? Does your program run with the user credentials to use the SQL server?

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      What errors are you getting?
      What have you tried so far?
      Do you know how to use an database in your program?
      If not maybe this article might help....

      -Frinny

      Comment

      • eranby
        New Member
        • Aug 2007
        • 17

        #4
        Hi,
        sorry for the lack of information I'm new to SQL server hence my problem could be something very simple such as configuration issue that I'm not familiar with.

        I used a string which I took from a web site I don't know if it is very helpful:
        SqlConnection con = new SqlConnection(" user id=username;" +
        "password=passw ord;server=serv erurl;" +
        "Trusted_Connec tion=yes;" +
        "database=datab ase; " +
        "connection timeout=30");
        trying to open connection with it results this run time 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 looked for this error but found nothing useful
        hope it help you to understand my problem please let kow if you need any other details

        thanks

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          That's close to the write connection string
          "Data Source=myServer Address;Initial Catalog=myDataB ase;User Id=myUsername;P assword=myPassw ord;"
          404 - Page Not Found. Shown when a URL cannot be mapped to any kind of resource.


          Scroll down to the SqlConnection (.NET) section

          Comment

          • eranby
            New Member
            • Aug 2007
            • 17

            #6
            I used the string you gave me and some others from the link but I'm still receiving the same error.
            are the parameters in the string should be changed in my application or they are the same for every connenction?
            and something about the error - it indicates that the server does not allow remote connection by default is it something in the configuration that I should set when adding the server?
            thanks

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Yes you will need to make sure to configure the SQL server to allow incomming connection.
              And I would hope you would know to change the parameter names from "username" to an actual username.
              What is the connection string you are using now?

              Comment

              • eranby
                New Member
                • Aug 2007
                • 17

                #8
                Hi ,
                thank you for your help.
                as I thought it was something very simple.
                I added reference to System.configur ation and add Using System.configur ation to the code and used this code in order to connect the server :
                ConnectionStrin gSettings settings =
                ConfigurationMa nager.Connectio nStrings["StockDB"];
                con = new SqlConnection(s ettings.Connect ionString);
                it solve the problem :)

                thanks again
                Eran

                Comment

                Working...