none
Accessing SQL table from C# application gives error-40 RRS feed

  • General discussion

  •  

    HI,

    I am new to SQL and C#. I am trying to connect to a SQL table which is a on a database named Math_Solve using a C# application.

    I wrote the codes after reading a few threads on the internet. My IP/TCP is enabled. firewall allows SQL and I am using windows authentification. My servername and user name in the SQL express log in do not mtach though(Is that a problem???). I am using SQL server express  2008 R2 and visual studio 2010 for this work. 

    Please help me to solve the problem.. many thanks in advance..

    The error:

    A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

    The code I have written:

    using

     

    System;

    using

     

    System.Data.SqlClient;

    class

     

    Demo

    {

     

    public static void Main(string

    [] args){

     

    try

    {

     

    SqlCommand comm = new SqlCommand

    ();

    comm.Connection =

    new SqlConnection("Data Source=(local);" + "Initial Catalog=Math_Solve;" + "Persist Security Info = true; Integrated Security = SSPI;"

    );

     

    String sql =

    @"SELECT Number,

    FROM Math_Solve

    "

     

    ;

    comm.CommandText = sql;

    comm.Connection.Open();

     

    SqlDataReader

    cursor = comm.ExecuteReader();

     

    while

    (cursor.Read())

     

    Console.WriteLine(cursor["Number"] + "\t"

    );

    comm.Connection.Close();

    }

    catch (Exception

    e){

     

    Console

    .WriteLine(e.ToString());

     

    Console

    .Read();

    }

    }

    }

    Thank you very much

    Please help me.

    Nick

    • Changed type NickTom Saturday, June 11, 2011 6:34 AM getting more suitable answers
    • Changed type NickTom Saturday, June 11, 2011 6:36 AM more suitable answers
    • Moved by VMazurModerator Tuesday, June 14, 2011 10:52 AM (From:ADO.NET DataSet)
    Saturday, May 28, 2011 1:55 PM

All replies

  • Hi Nick;

    When SQLExpress is installed it is an instance of (local), add the instance name to your connection string as shown below.

    comm.Connection = new SqlConnection(
    	"Data Source=(local)\SQLEXPRESS;" + 
    	"Initial Catalog=Math_Solve;" + 
    	"Persist Security Info = true; Integrated Security = SSPI;";
    
    


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, May 28, 2011 5:48 PM
  • Hi Fernando,

     

    I tried as you suggested. But now it gives me a build error in the application. A red wiggle comes under the letter "S" in the "SQLEXPRESS" part of the connection string. The build error is "Unrecognised escape sequence"

    My new connection string looks like this now.

    comm.Connection = new SqlConnection(
    "Data Source=(local)\C-703-112-NICK\SQLEXPRESS;" +
    "Initial Catalog=Math_Solve;" +
    "Persist Security Info = true; Integrated Security = SSPI;";

    This time it gives no error though. But the output is so many lines like

    at.system.data.sqlclient.sqlconnectionfactory.crateconnection<dbconnection options options, object poolgroupproviderinfo....etc)

    What do you think the problem is?

    Man thanks for the advise and help.

    Nick.

    Sunday, May 29, 2011 12:10 PM
  • Hi Nick;

    When we define a string in C# and have a \ in the string that does not define a control character the opening quote mark must be preceded by a @ character or use \\ in place of a single \ character. So the connection string should look like the below string.

    comm.Connection = new SqlConnection(
      @"Data Source=(local)\C-703-112-NICK\SQLEXPRESS;" 
      + "Initial Catalog=Math_Solve;" 
      + "Persist Security Info = true; Integrated Security = SSPI;";
    

    When defining the Data Source in the connection string it should be, Host Name\ Instance or (local)\Instance name. So in your connection string I see, (local)\C-703-112-NICK\SQLEXPRESS, if C-703-112-NICK is the machine name then your connection string should be :

    comm.Connection = new SqlConnection(
      @"Data Source=C-703-112-NICK\SQLEXPRESS;" 
      + "Initial Catalog=Math_Solve;" 
      + "Persist Security Info = true; Integrated Security = SSPI;";
    

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, May 29, 2011 2:47 PM
  • Hi Fernando,

    Your answer helped my problem. Now I my application is correctly connected to the SQL server.

    THANK YOU VERY MUCH :)

    Have a nice day!

    Nick..

    Monday, May 30, 2011 6:57 AM
  • Not a problem, always glad to help Nick.
    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, May 30, 2011 1:52 PM
  • Hi Nick;

    Seeming we solved this issue can you please close the question.

    Thank you and have a great day;
    Fernando

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, June 11, 2011 1:23 PM
  • Hi Fernando,

     

    That was by mistake.. Yes you provided the solution for the problem.

    Sorry about it.

    Many thanks

    Nick

    Wednesday, June 15, 2011 1:30 PM
  • Hi Nick;

    Seeming that you are new to MSDN forums and may not know how to close / Mark as Answer a question please read the below from the MSDN help page.

    Why should I indicate a post answered my question?
    When someone adds a post to a question you asked, you can rate this post as "the answer" to your question. By rating a post as "the answer", you acknowledge the contribution of the person who posted the answer, help others find the answer quickly, and steer further discussions in the right direction.

    How do I indicate a post answered my question? To mark a post as answer, click Mark as Answer beneath the post. You can unmark a post as answer by clicking Unmark as the Answer.

    How do I rate a post? To rate a post as helpful click the Vote as Helpful on any post. You can vote only once.

    Thank you for your assistance.

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, June 15, 2011 2:14 PM
  • Hi Fernando,

    Yes I am new to MSDN and I just wanted to check how "Change Type" would work. I did not want to belittle your effort in helping me to find the solution. Your solution really helped me to solve the problem and I am really sorry if my poor knowledge on MSDN hurt your feelings.

    Thank you very much for providing me a good understanding on how the MSDN forum works.

    Have a nice day :)

    truly

    Nick

    Thursday, June 16, 2011 8:19 AM
  • Hi Nick;

    It was not a problem and please I am sorry if I some how made you feel that you had offended me, you did not. Always feel free to come back to the forums here for help any time you need.

    Have a great day;
    Fernando


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, June 16, 2011 2:22 PM
  • HI,

     

    that is really a comforting thought. :) I have another problem which I have posted in a different thread regarding writing some output on to seperate columns of a CSV file. Please let me know if you can help me.

    Wish you all the best with everything:)

    Truly

    Nick.

    Thursday, June 16, 2011 3:01 PM