SQL Server connection string troubles - sql-server

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

Related

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.

How to access a mdf file without Sql Server

I'm working on one Windows forms application using one .mdf file. And I have Sql Server 2008 installed in my machine , it is working fine in my machine. Now I need to install this application in Client's machine, Client's machine don't have Sql Server if they try to access this getting Network related exception, ie,
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)
and this is my connection string in app.config file,
<add name="CustomConnection" connectionString="Integrated Security=True;MultipleActiveResultSets=True;Connect Timeout=120;User Instance=True;AttachDbFilename=C:\Users\[username]\AppData\Roaming\Smart\App_Data\SampleDB.mdf" providerName="System.Data.SqlClient" />
Can we access the .mdf file without Sql Server instance? If yes how can we set the ConnectionString value.
Thanks,
Abhishek

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

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

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

Local IIS Web server cannot connect to SQL Server

When I use the local IIS Web server, my ASP.NET MVC application is unable to connect to SQL Server. The database is on another server. However, if I use the Visual Studio Development Server, it works just fine.
Anyone know what settings I'm missing? Should something change in my Web.config?
Yes, TCP/IP is enabled on SQL Server.
Here is the error I get -
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)
Here is my connection string -
connectionString="
SERVER=servername;
Initial Catalog=dbname;
User ID=userid;
Password=passwd"
providerName="System.Data.SqlClient"
I think you need to use IP address to connect remote database something like SERVER=59.230.212.12 in connection string. Try to connect database in management studio from where your IIS is installed . then use same setting in connection string .

Resources