Connecting to Sql Server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manisha khabrani
    New Member
    • Jun 2007
    • 24

    Connecting to Sql Server

    i am currently using sql server 2000 and working with C# database. i am getting the following error "sql server does not exist or access denied. ExecuteNonQuery requires an open and available connection " plzzzzzzzzzzz help me, i was going through some articles which said look out for the IP address of sql server and which i could not figure out. i just need my connection to be established, that's it. my connection string is also correct, hopefully.
    thanking u in anticipation.
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by manisha khabrani
    i am currently using sql server 2000 and working with C# database. i am getting the following error "sql server does not exist or access denied. ExecuteNonQuery requires an open and available connection " plzzzzzzzzzzz help me, i was going through some articles which said look out for the IP address of sql server and which i could not figure out. i just need my connection to be established, that's it. my connection string is also correct, hopefully.
    thanking u in anticipation.
    Please post your code so that we can have an idea of what you are doing?

    Nathan

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by manisha khabrani
      i am currently using sql server 2000 and working with C# database. i am getting the following error "sql server does not exist or access denied. ExecuteNonQuery requires an open and available connection " plzzzzzzzzzzz help me, i was going through some articles which said look out for the IP address of sql server and which i could not figure out. i just need my connection to be established, that's it. my connection string is also correct, hopefully.
      thanking u in anticipation.
      Hi Manisha, welcome to TSDN!
      In the future could you please use a more descriptive title explaining your problem.
      Thanks!

      As for your problem, have you checked out this .net article on how to use a database in your program ? I know its in VB.NET but it can easily be translated into C#.

      Could you please post the code snippet that you're using to connect to your database so that we can better help you (please use the [code] tags to indicate that your code is code.)

      Thanks!

      -Frinny

      Comment

      • manisha khabrani
        New Member
        • Jun 2007
        • 24

        #4
        Originally posted by nateraaaa
        Please post your code so that we can have an idea of what you are doing?

        Nathan
        firstly, thank u sooooooooo much Nathan for having a look at my question. i really appreciate it, wish if I could be of some use to u too. bless u.

        here is my code, which happens to be my first code using a database.

        Code:
        using System;
        using System.Data;
        using System.Data.SqlClient;
        
        
        namespace ndatabase1
        {
        	
        	class Class1
        	{
        	public static void Main(string[] args)
        		{
        			//SqlConnection myConnection = new SqlConnection("user id=sa;" + 
                      //                         "password=sql;" + "Data Source=localhost;" +
          //
            //                                     "Trusted_Connection=yes;" + 
              //                                 "Initial Catalog=northwind; " + 
                //             "connection timeout=30");
        
            SqlConnection myConnection = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
        
        	try
        		{
        		  myConnection.Open();
        		}
        	catch(Exception e)
        		{
        			Console.WriteLine(e.ToString());
        		}
        
        
        		SqlCommand myCommand = new SqlCommand("INSERT INTO names" +
        			                                    "VALUES ('aditi' 28)", myConnection);
        
        
                myCommand.ExecuteNonQuery();
        
        	try
        		{
        			 myConnection.Close();
        		}
        	catch(Exception e)
        		{
        		Console.WriteLine(e.ToString());
        		}
        
        
        
        
        			
        		}
        	}
        }


        I followed the following 6 steps from the MSDN help :
        To compile and run the ADO.NET sample application

        Using Notepad or another text editor, create a blank text file named sample.vb for Visual Basic sample code or named sample.cs for C# sample code.
        Copy and paste the Visual Basic or C# sample code from this topic into the blank text file. Save the file.
        Open a command prompt (Start, then Run, then enter "command").
        In the command prompt, change the directory to the directory that contains the new text file. For example:
        cd\SampleCode\A DONETSample
        In the command prompt, enter one of the following commands to compile the sample (the path to your file will likely differ).
        For Visual Basic, use vbc.exe and use the following command to reference the system libraries needed to run your ADO.NET application.
        vbc.exe sample.vb /r:System.dll /r:System.Data.d ll /r:System.Xml.dl l
        For C#, use csc.exe and use the following command to reference the system libraries needed to run your ADO.NET application.
        csc.exe sample.cs /r:System.dll /r:System.Data.d ll /r:System.Xml.dl l
        The compiler will create an executable called sample.exe. Enter "sample.exe " at the command prompt to run the compiled sample.



        Nathan it would be gr8 if u could help me out with this plsssssssssssss ssss.
        thanks......

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Hi Manisha,

          Is your SQL Server set up to use Windows authentication for your database user or SQL Authentication?

          -Frinny

          (ps. I added [code] tags around your code...see how nice it looks?)

          Comment

          • manisha khabrani
            New Member
            • Jun 2007
            • 24

            #6
            Originally posted by Frinavale
            Hi Manisha, welcome to TSDN!
            In the future could you please use a more descriptive title explaining your problem.
            Thanks!

            As for your problem, have you checked out this .net article on how to use a database in your program ? I know its in VB.NET but it can easily be translated into C#.

            Could you please post the code snippet that you're using to connect to your database so that we can better help you (please use the [code] tags to indicate that your code is code.)

            Thanks!

            -Frinny

            hey Frinny, this is my first day on this site. u guys are so good and friendly, I really appreciate it, its quiet astonishing, u all are so calm , kind and helpful. god bless u, thanks plenty.........

            Comment

            • manisha khabrani
              New Member
              • Jun 2007
              • 24

              #7
              Originally posted by Frinavale
              Hi Manisha,

              Is your SQL Server set up to use Windows authentication for your database user or SQL Authentication?

              -Frinny

              (ps. I added [code] tags around your code...see how nice it looks?)

              hey Frinny,
              while selection the authentication I was asked to select "Mixed Mode" and not windows authentication and I personally also wanna use mixed mode. what can be done now? BTY i dont know how to tag around the code.
              thanksssssssss

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by manisha khabrani
                hey Frinny, this is my first day on this site. u guys are so good and friendly, I really appreciate it, its quiet astonishing, u all are so calm , kind and helpful. god bless u, thanks plenty.........
                I'm glad you like it here!
                Hopefully we can get you through your problem.

                -Frinny

                Comment

                • manisha khabrani
                  New Member
                  • Jun 2007
                  • 24

                  #9
                  Originally posted by manisha khabrani
                  hey Frinny,
                  while selection the authentication I was asked to select "Mixed Mode" and not windows authentication and I personally also wanna use mixed mode. what can be done now? BTY i dont know how to tag around the code.
                  thanksssssssss

                  hey Frinny,

                  what happened, am I actually disturbing u at work ? actually i am so anxious to know the reply that I can stop myself.

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Originally posted by manisha khabrani
                    hey Frinny,

                    what happened, am I actually disturbing u at work ? actually i am so anxious to know the reply that I can stop myself.
                    I'm still here to help you out.

                    Ok the mixed authentication mode means that you can log into the SQL Server using the current windows user logged into the system. If you are developing an web application this user is the ASP.NET user and you really don't want them to have access to your database.

                    It is a good idea to set up a database user for your SQL server specifically for your application. When you do this you will specify that this user will user SQL authentication. ..give it a user name and password here.

                    Then use the user name and password in your connection string in your code...but you will have to change your connection string to be something like:
                    Code:
                    connectionString="data source=northwind;initial catalog=nameOfDatabase;user id=userId;password=pass"
                    -Frinny

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Just so you know, normally you don't get responses this quickly.
                      I happen to be logged in and paying attention to your thread right now. Normally it can take up to a day to receive any help here on theScripts.

                      Comment

                      • manisha khabrani
                        New Member
                        • Jun 2007
                        • 24

                        #12
                        Originally posted by Frinavale
                        I'm still here to help you out.

                        Ok the mixed authentication mode means that you can log into the SQL Server using the current windows user logged into the system. If you are developing an web application this user is the ASP.NET user and you really don't want them to have access to your database.

                        It is a good idea to set up a database user for your SQL server specifically for your application. When you do this you will specify that this user will user SQL authentication. ..give it a user name and password here.

                        Then use the user name and password in your connection string in your code...but you will have to change your connection string to be something like:
                        Code:
                        connectionString="data source=northwind;initial catalog=nameOfDatabase;user id=userId;password=pass"
                        -Frinny


                        wow, in that case i am very lucky to have a pro around!!

                        but i am a dumbo , i did not understand much of the reply u had sent me, it was quiet technical and i am quiet new. welll in this code whatever is marked in comments // was actually what u are asking me to do again. i had put the user name and password b4. currently i have written this code in console application.

                        Comment

                        • Frinavale
                          Recognized Expert Expert
                          • Oct 2006
                          • 9749

                          #13
                          Originally posted by manisha khabrani
                          wow, in that case i am very lucky to have a pro around!!

                          but i am a dumbo , i did not understand much of the reply u had sent me, it was quiet technical and i am quiet new. welll in this code whatever is marked in comments // was actually what u are asking me to do again. i had put the user name and password b4. currently i have written this code in console application.
                          I'm sure your not as dumb as you think....everyo ne starts somewhere...and even the so called "Pros" have their moments of relapse from time to time.

                          So, tell me about your database then....have you set it up like the example you were following?

                          Comment

                          • manisha khabrani
                            New Member
                            • Jun 2007
                            • 24

                            #14
                            Originally posted by Frinavale
                            I'm sure your not as dumb as you think....everyo ne starts somewhere...and even the so called "Pros" have their moments of relapse from time to time.

                            So, tell me about your database then....have you set it up like the example you were following?

                            based on what u said , this is what i did in the code , which unfortunately did not work.

                            1.SqlConnection myConnection = new SqlConnection(" data source=localhos t;initial catalog=northwi nd;user id=sa;password= sql");

                            and yes today i was following the steps written in the book and managed to set up sql server 2000 because it had got corrrupted it seems. i am equally new to sql too.

                            and rite now i am completely fed up with databases, its 2 am here rite now.

                            and (so many and's) THANK U SO MUCH. i wish if i could be of some help to u, seriously. you were very patient throughout. may all your dreams come true...

                            Regards,
                            Manisha

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              You're probably just frustrated because its 2am there!!
                              I know I can't think straight when I'm tired.

                              Try again in the morning ;)

                              -Frinny

                              Comment

                              Working...