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

Broken_Code
  • 306
  • 5
  • 19
  • Is your database server hosted on the same machine as you are developing on? – IrishGeek82 Jun 09 '14 at 07:39
  • Check out [http://connectionstrings.com](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 – marc_s Jun 09 '14 at 07:40
  • @irishGeek82: yes I am just learning at the moment – Broken_Code Jun 09 '14 at 07: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 – Broken_Code Jun 09 '14 at 08:00

3 Answers3

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

Community
  • 1
  • 1
Marek
  • 3,555
  • 17
  • 74
  • 123
  • 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 Jun 09 '14 at 08:06
  • @Broken_Code when using `Intergrated security = false`you need to specify user and password. – Marek Jun 09 '14 at 08:09
  • Like I said I am not using one how do i fix this – Broken_Code Jun 09 '14 at 08:09
  • @Broken_Code post here your current SQL connection please. – Marek Jun 09 '14 at 08:10
  • SqlConnection Conn = new SqlConnection(@"Server=Andile-Pc;Database=nameDb;Integrated Security=false;"); – Broken_Code Jun 09 '14 at 08:12
  • @Broken_Code try `"Server=Andile-Pc;Database=nameDb;Integrated Security=true;"` or `@"Server=Andile-Pc;Database=nameDb;user=user;password=yourpass"` – Marek Jun 09 '14 at 08:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55305/discussion-between-marek-and-broken-code). – Marek Jun 09 '14 at 08:34
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.

coder
  • 13,002
  • 31
  • 112
  • 214
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"
sdrzymala
  • 387
  • 1
  • 10
  • I have tried the code however I am still told by the configuration manager that "the remote procedure failed' – Broken_Code Jun 09 '14 at 08:09
  • 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? – sdrzymala Jun 09 '14 at 08:12
  • 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 – Broken_Code Jun 09 '14 at 08:15
  • 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? – sdrzymala Jun 09 '14 at 08:19
  • 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 – Broken_Code Jun 09 '14 at 08:23