Skip to content

How to Connect to SQL Server 2008 with VS 2010

natchaya khamkuna

Asked May 1, 2011, 7:18 AM EDT4 answers
Share
LinkedInX
I need help.
  I use Visual studio 2010 and CSharp  try to connect to SQL Server (Developer edition install into my computer) 

string conStr = "Data Source=.\\MSSQLSERVER;Initial Catalog=AdventureWorks;Integrated Security=True";

SqlConnection conn = new SqlConnection(conStr);
conn.Open();

but it has error like below.
---------------------
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
-------------------------
I also enable the  remote server connection option.

So, please help suggest me if you can.

Thank you

foonMod

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mahesh Chand
    0
    To get the correct connection string, open Server Explorer and add a new connection with the database you are trying to use. That will get you the correct connection string. If there is no server available in the servers list, there is some network or access error.
    0
  • 0
    Thank you Mr. Mahesh Chand.  It's great I can add new connection with the database I need by use the Server Explorer. But, Where I can find the  connection string ?
    0
  • 0
    Hi Mr. Mahesh Chand ,
        I got it now , Thank anyway.. 

    FoonMod
    0
  • Abhimanyu K Vatsa
    0
    plz follow the steps

    (i) open the Server Explorer and refresh the databases
    (ii) if you get any error at refresh time then most probably your system has some temp. files or say bugs which is creating error
    (iii) if database refreshes well then proceed next
    (iv) add another .aspx page and switch it in design view
    (v) drag the table from db explorer on web form
    (vi) it automatically will create a new connectionString in your config file or already exist then  override it. 
    (vii) now use that connectionString name in your apps


    hope this helps
    0