How to connect to sql server on c# application - c#

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:
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
I would also like to add that I added sql server to the firewall exceptions already

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

Check whether these services has been started or not as shown in below snapshot
..
If then try to start them and please try to connect your database.

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"

Related

Connection string for different MS SQL servers [duplicate]

This question already has an answer here:
SQL Server Connection Strings - dot(".") or "(local)" or "(localdb)"
(1 answer)
Closed 5 years ago.
I have several SQL servers in my system:
2017 MSSQLSERVER
2008 SQLEXPRESS
In SSMS I see them like:
GM\
GM\SQLEXPRESS
If I need to connect to SQLEXPRESS I use connection string with . in Server:
connectionString="Server=.;Database=LearnCSharp;Integrated Security=True"
But what does . means in my case and what should be placed in connection string in order to connect to 2017 MSSQLSERVER?
UPD.:
. connects to 2017 server. I have noticed that before computer restart it was connecting to 2008 server
GM connects to 2017 server.
.\MSSQLSERVER generates exception in my application;
System.Data.SqlClient.SqlException (0x80131904): 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: 25 - Connection string is not valid) ---> System.ComponentModel.Win32Exception (0x80004005): The parameter is incorrect
.\SQLEXPRESS and GM\SQLEXPRESS - both connects to server 2008
It means localhost or "this machine"
To specify an instance name, use a backslash after the dot (or computer name):
.\SQLEXPRESS
YOURCOMPUTER\SQLEXPRESS
OTHERCOMPUTER\SQLEXPRESS
etc
Remember when this app is deployed to another machine, the value of "this machine" changes. This may or may not be what you want, so watch for it in future
It's also worth noting that the default SQL Server instance cannot be referenced by name, and if you try to give its name the connection won't work out. If you know you want to connect to the default SQL Server instance (whichever one that is, and there can be only one) then don't put a slash or a name, just a reference to the machine:
.
YOURCOMPUTER
etc
The dot is simply an abbreviated way of referencing your local machine and as highlighted in the following ".\MSSQLSERVER" should do it: SQL Server Connection Strings - dot(".") or "(local)" or "(localdb)").

Can't open connection to SQL server

When I try to connect to the MS SQL server in the local domain with SQL with SQL server authentication using the following code fails:
SqlConnection sql = new SqlConnection(conString);
sql.Open();
sql.Close();
I can connect to my local SQLExpress test database just fine, using either windows or sql authentication. I tried using many different connections strings for the domain database, including using the connection string generated by (successfully) adding the server as a DataSource. What am I doing wrong?
EDIT: When I add the server in the server explorer of visual studio, I can connect successfully. I would assume that the connection string thereby generated is valid:
Data Source=mySubDomain.myDomain.local;Initial Catalog=myDatabase;Persist Security Info=True;User ID=myUser;Password=myPassword;
I checked and remote connections are allowed. I do not have permission to access the windows server which the sql server is running on, so I can't check any further settings.
EDIT 2: The following message comes with the exception:
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)
EDIT 3:
I tested my application on a different computer in the same domain, and it worked. Does someone have a clue what's going on or how I could find out?
The Group Policy or firewall (or both, not sure) of this company domain make my .exe file (and any other .exe) behave in the following way:
Launching it from C:\Program Files (or C:\Program Files (x86)) as any user works fine
Launching it from anywhere else as a normal user is not allowed and fails
Launching it from anywhere else as a local admin will execute the application, but connections are blocked

cannot connect sql server 2008 from another computer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
System.Data.SqlClient.SqlException A network-related or instance-specific error
i have winform application in c# which connect to my own db and its working fine my current sql connection string is :
SqlConnection con = new SqlConnection("Data Source=ITPL_PC1;Initial Catalog=Data_Project;Persist Security Info=True;user id=sa;Password=insforia");
but when i am taking this winform to another computer its not working .. then i am changing my connection string to :
SqlConnection con = new SqlConnection("Data Source=192.168.0.28\\ITPL_PC1;Initial Catalog=Data_Project;Persist Security Info=True;user id=sa;Password=insforia");
but its not working ...
i have changed all the settings in sql server of remote access but still it showing an error
Sql Exception was unhandled
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)
is my connection string is wrong? what it should be?
pls help me out
Data Source=ITPL_PC1
Identifies a server called ITPL_PC1. You're connecting to the default instance on this server.
Data Source=192.168.0.28\ITPL_PC1
Identifies an instance called ITPL_PC1 running on a server with IP address 192.168.0.28. We don't know the name of this server.
If the instance you want to connect to is the default instance on ITPL_PC1, then the first version should always work (provided ITPL_PC1 can be resolved).
The Windows security might be preventing the access. You can try the below.
1.Go to the service management console of windows by typing services.msc in the run window.
2. In the window displayed you can see a list of services. From the list find out Base Filtering Engine.
3.Right click and stop it. Its done
I have fixed the same issue like this after lot of googling.(For me it was not working even after enabling TCP/IP from sql configuration manager)
First you open SQL managnebt studio and connect to another pc sql server :
Please follow bolew step :
Strat -> program -> window sql server 2008 -> configuration tools
--> sql server Configuration manager -->
Start sql server browser service
-----------
Next --->Sql serverNetework COnfiguration --> Enable all Protocls and
also Sql Native Client.. --> Enable all Protocls then
--------------
reatsrt all service and connect to sql
-------------------------------------------------
If you get connection then SQL Database to connect database from visual studio from there to find connection string and copy and paste to connection string.

Connecting to a database

I'm trying to connect to a database but nothing I try works.
SqlConnection conn = new SqlConnection(#"Data Source=C:\Users\Gerard Foley\Desktop\Northwind.sdf");
conn.Open();
No matter what I try I just 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 stole the connection string from Database Explorer -> Properties -> Connection String. What am I missing? I can get the tables to show up in a DataGridView fine (by dragging from Data Sources), but I want to use my own UI and queries. I just can't seem to figure this ADO thing out.
Using c# express 2010 and sql server express 2008.
for the proper connection string to use to connect to SQL Server have a look at:
http://connectionstrings.com
the connection string you are using now is strange, it should contain server name and database name, see link above for examples...
GOT IT. I should have been using Sql*Ce*Connection. The connection string was fine.
You need to specify 'AttachDbFileName' in the connection string. See the examples for sql server express here: http://www.connectionstrings.com/sql-server-2008.

SQL Server connection issue

I am using VSTS 2008 + .Net 3.5 + C# + SQL Server 2008 Enterprise on Windows Server 2003. I am using the following connection string, and labtest1 is the local machine name and I connect from local machine using ADO.Net. Then it always fail with connection error. But when I change in the connection string from "labtest1" to ".", connection has no issue with the same ADO.Net client code. Any ideas what is wrong?
Data Source=labtest1;Initial Catalog=CustomerDB;Trusted_Connection=true;Asynchronous Processing=true
Here is the detailed error message I got,
System.Data.SqlClient.SqlException: 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)
Looks like a Sql Server configuration issue to me : have you tried to tune protocols in Sql Server Network configuration ? Named Pipes or TCP/IP should be enabled.
When you use "." or "(local)" it connects to the default instance on your PC, perhaps the SQL Server was installed with instances, in which case you have to specify the instance name in the connection string in the format "SERVER\INSTANCE_NAME".
In SQL Management studio execute this query to see your full server\instance name
select ##servername
I have seen this issue previously with ZoneAlarms blocking the connection (on the machine trying to connect to the SQL server). I would spend some time investigating this area around firewalls etc.
Hope this helps
Do you have the Named Pipes network protocol enabled in the network config? (In the SQL Server Configuration Manager - sql server 2005, that's what I have, might be different in 2008 - you should be able to verify this setting)
Try to ping by the computer name: ping labtest1. if it does not find the server, then try ping labtest1.mydomainname.com with the domain name. If this works, then you just need to add/fix the DSN aliases in the domain controller or just re-login to the machine.
One other thins - and may not be relevant - but you've not specified the security model, at least not in the string provided as a sample.
I would expect to see: Integrated Security=True (given that its the local machine) in the connection string.
The other thing that may be relevant is - as has already been mentioned - the protocols, I'd look to make sure that TCP/IP is enabled.

Categories