2

I can connect asp.net applications to my databases in SQL Server 2008, however when I try to do the same from Visual Studio in a Winforms application, I always get 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"

I use the connection string:

 SqlConnection Conn = new SqlConnection(@"Server=.\\myserver address\user;Database=testDatabase");

and I get an error when my form loads as that is when I start the connect.

I have gone to my SQL Server Configuration Manager and I get this:

enter image description here

So I do not know what I am doing wrong. There are previous files for older versions of SQL Server in the program files however I do not see how they could affect it...

My sql services

enter image description here

I would also like to add that I added sql server to the firewall exceptions already

4
  • Is your database server hosted on the same machine as you are developing on? Commented Jun 9, 2014 at 7:39
  • Check out http://connectionstrings.com for a ton of samples on what connection strings look like etc. You're not specifying what user credentials to use to connect to SQL Server right now - that might be the issue Commented Jun 9, 2014 at 7:40
  • @irishGeek82: yes I am just learning at the moment Commented Jun 9, 2014 at 7:59
  • @marc_s: I have used that and it did not work so i went on youtube found a video and tried following that which is not working and is the code you see above, Even solutions i download and after changing the string to match my own do not work Commented Jun 9, 2014 at 8:00

3 Answers 3

1

You should consider using connection with following syntax

Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;Integrated Security=SSPI;

OR

Server=myServerName\myInstanceName;Database=myDataBase;Integrated Security=true;

When using current Windows account credentials

OR on local Server with default Windows authentication

Server=myServerName;Database=myDataBase;Integrated Security=true

(this was solution that worked for OP)

And for @"Server=.\\myserver using @ before string means that no character would escape that string so

\\ without @ turns into \

\\ with @ stays \\

check C# '@' before a String

For more information about connection strings please visit connectionstrings.com

Sign up to request clarification or add additional context in comments.

7 Comments

You code appears to connect me to the database however I am getting a log in error, I did not add a password and I switched intergrated security to false but still get a log in error what am I doing wrong
@Broken_Code when using Intergrated security = falseyou need to specify user and password.
Like I said I am not using one how do i fix this
@Broken_Code post here your current SQL connection please.
SqlConnection Conn = new SqlConnection(@"Server=Andile-Pc;Database=nameDb;Integrated Security=false;");
|
0

Check whether these services has been started or not as shown in below snapshot

enter image description here..

If then try to start them and please try to connect your database.

1 Comment

I have updated my question and all my sql services are currently running.
0

Please try to use the folowing command: I can see that you may have a problem with MOF.

mofcomp.exe "C:\Program Files (x86)\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof"

5 Comments

I have tried the code however I am still told by the configuration manager that "the remote procedure failed'
It's not a problem with a C# code but SQL Server. Which version of Windows? Did your server was working and suddendly "die" or it's just after the installation? edit: Are you able to connect to your database from SQL Server Managemnt Studio?
i am using windows 7 ultimate 64 bit and running sql server 2008 r1, No from the first time I installed it i have had this problem initially i had thought it was just my code because my asp.net applications are easily able to connect to the database and save and pull information however my c# winform applications can not connect to it
Are you sure that your ASp.net application was connecting to this database and not to the SQL Server express edition? Cna you connect to this server via Sql Server Management Studio?
No not this database it was connecting to a different database. yes I am able to connect to this database through sql server management studio very easily the problem only arises when I try to connect my winforms to the databases

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.