Why is my connection to a local SQL Server db failing? - sql-server

Based on what I learned here, I'm using this connection string:
using (var conn = new SqlConnection(#"AttachDBFilename=C:\HoldingTank\AdventureWorksLT2012_Database\AdventureWorksLT2012_Data.MDF;Integrated Security=True;User Instance=true"))
...to (attempt to) attach to a local SQL Server db, but I get this exception at runtime:
System.Data.SqlClient.SqlException was unhandled by user code
HResult=-2146232060
Message=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)
Source=.Net SqlClient Data Provider
Based on my legacy MS Access connection string, I had previously also had:
Provider=System.Data.SqlClient;
prior to the "AttachDBFilename=..." part of the connection string, but that caused an exception of its own...

You are missing a few values in the connection string. Here is one I used recently:
"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
I'm thinking the most likely culprit is the lack of the "Initial Catalog" value.

The connection string you need will depend on several factors, such as the edition of SQL Server (LocalDB/Express/Standard), whether or not it's a named instance, and the type of authentication (SQL vs Integrated) you have in place.
The answer from #zippit is a good example of a connection string for LocalDB using integrated security.
The same string to a Sql Express server would look like this:
"Data Source=serverNameOrIpAddress\sqlepxress;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
..and to a standard edition of Sql Server would look like this:
"Data Source=serverNameOrIpAddress\sqlepxress;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
..and to a named instance on a standard edition of Sql Server would look like this:
"Data Source=serverNameOrIpAddress\instanceName;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
All those assume integrated authentication. If you have sql authentication set up, you would substitute "Integrated Security=True" with "User Id=username; Password=pword;"
Also, if the sql server is on the same machine, you can use this for the Data Source parameter for Sql Express
.\sqlexpress
..and this for standard Sql Server
(local)
Here's a site I've found useful: Connection Strings

Related

SQL Server connection string troubles

I am working on an ASP.NET MVC project, which uses this connection string:
<add name="Name"
connectionString="Data Source=.;initial catalog=MyDBName;integrated security=True;multipleactiveresultsets=True;" />```
Note: the data source value is ., this works but after I reset my pc and reinstalled SQL Server, it no longer works, I get an 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.
until I change the . in the connection string to DESKTOP-1V12LN7\SQLEXPRESS.
My question is what does the value . in the connection string mean and why doesn't it work after reinstalling SQL Server?
The data source=. (or server=. or data source=(local), which are all equivalent) means : connect to the local machine and the default, unnamed instance of SQL Server - this is the default for all non-Express editions of SQL Server installed on your machine.
When you install SQL Server Express, by default, it doesn't get installed as the default, unnamed instance - but it uses a SQLEXPRESS instance name - so you need to change your connection string to:
.\SQLEXPRESS
(local)\SQLEXPRESS
DESKTOP-1V12LN7\SQLEXPRESS

IIS can't connect to SQL Server, but an external PC can

My VB.Net code connects to SQL Server. On my PC, it succeeds. On IIS running on the same machine as SQL Server, it fails. Any ideas?
I've tested:
I've tried IP address and server name. Same result.
I've tried Integrated Security and I've tried username/password. Same result.
On the server machine, Excel and SSMS can connect to SQL server using the same server name, username and password.
The fact that my PC can connect excludes many common problems including: ip address, server name, port, firewall, turn on sql server, connection string, credentials, permissions, sql roles.
Server (IIS and SQL) info: VB.Net, connection string providerName="System.Data.SqlClient", Windows Server 2008 SP2.
My PC info: Run website in Visual Studio 2019. Connects to same DB using same connection string (not local db and not as a different user).
Code (fails on second line):
oConn = ConnectionService.DatabaseName.Connect
oConn.Open()
Connection string: connectionString="Data Source=IP Address\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=False;Persist Security Info=False;User ID=Username;Password=Password;" providerName="System.Data.SqlClient"
Error message: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): The network path was not found
Oops. Right after posting this, I found out Web.Config pointed to a different connection string. That should explain and solve everything. I had checked Web.Config, but it turns out that that wasn't the one this server was using.

SQL Server 2014 connection string with instance name

My working SQL Server 2014 connection string is:
Data Source=localhost;Initial Catalog=myDb;Integrated Security=True;
I need to install a new instance of SQL Server 2016 on the same server. Therefore I need to modify the existing connection string and to add the instance name.
I was trying (MSSQLSERVER is the instance name):
"Data Source=localhost\MSSQLSERVER;Initial Catalog=myDb;Integrated Security=True;" providerName="System.Data.SqlClient"
AND:
"Server=localhost/MSSQLSERVER;Database=myDb;User Id=user; Password=password;" providerName="System.Data.SqlClient"
AND more but could not make it work.
The error I am getting is:
The network name cannot be found
If you have SQL Server 2014 as your default instance (with no instance name needed to connect to it - that's the MSSQLSERVER "instance", but that name must not be used in the connection string!), then you must use a separate, different instance name for your SQL Server 2016 installation, e.g. SQL2016.
In that case, your connection string will need to use .\SQL2016 or (local)\SQL2016 or localhost\SQL2016 as the server/instance name (defined by the server= or data source= settings in the connection string).
So your connection string for SQL Server 2016 should be something like:
Data Source=localhost\SQL2016;Initial Catalog=myDb;Integrated Security=True;
You can go to the SQL Server Configuration Manager to see what services are defined and thus what instances are present on your machine:
Look for the SQL Server services - the value in parenthesis is the instance name (where MSSQLSERVER stands for the default instance that doesn't need to be specified as such - just the machine name is enough for connecting to the default instance)

SQL Server Express connection string error

Trying to figure out the correct connection string for a SQL Server Express database. Trying to publish my app to IIS and test before publishing to my hosting site.
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 started here and also used connectionstrings.com. I also started reading the deploy asp.net app using SQL Server Compact article here
Using this connection string
Data Source=.\\SQLExpress;AttachDbFilename=H:\DB\Guestbook.sdf;Integrated Security=True
I get the above error
This connection string
<add name="DefaultConnection"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI; database=Guestbook; AttachDBFilename=|DataDirectory|Guestbook.sdf"
providerName="System.Data.SqlClient" />`
throws a trust level error. I've changed to medium and full and still get one of the two errors.
I've tried a variation of a few different strings with no luck.
Thanks
Please verify that SQL Service is started in services.msc and use the complete instance with the hostname ej: HOSTNAME\SQLEXPRESS
The file extension indicates that this is not a SQL Server Express database, so you must use:
Data Source=<Full path to file>
or
Data Source=|DataDirectory|Guestbook.sdf

How to connect to SQL Server 2000 from .net 4

Here's my connection string...
Data Source=MYMACHINE\SERVER2000; Initial Catalog=MyDatabase; User Id=sa; Password=p;
Here's 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 can connect fine with Enterprise Manager. Also I have upgraded the SQL Server to Serivce Pack 4.
Any ideas what the issue might be?
I'd suggest testing your connection string using a .UDL file. I've answered a similar question here. After creating your connection, you can rename the extension of the file to .txt, open it and then copy and paste the working connection string in.
string connectionString=#"Data Source=MYMACHINE\SERVER2000; Initial Catalog=MyDatabase; User Id=sa; Password=p;";
SqlConnection connection=new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("Select * From Table",connection);
SqlDataReader reader= command.ExecuteReader();
GridView1.DataSource=reader;
GridView1.DataBind();
Use this.... this may help you?

Resources