SQL Server 2014 connection string with instance name - sql-server

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)

Related

How is SQL Server mapping my server using a "." as the server name?

I am looking at an existing configuration. One SQL Server and I can connect like this:
myServer (SQL Server 1.1.1.2 - personal)
Data Source=myServer;Initial Catalog=mydatabase;Persist Security Info=True;User ID=user;Password=pass;
or like this:
. (SQL Server 1.1.1.2 - personal)
Data Source=.;Initial Catalog=mydatabase;Persist Security Info=True;User ID=user;Password=pass;
What I don't understand is how this is setup to have the "." map to the server name "myServer"?
I tried this on a local instance of SQL Server that I have setup and the "." doesn't map to the local instance of SQL Server that I have, so I'm assuming there is some sort of mapping done on the server I'm working with?
Nothing useful in ODBC 32/64 bit client either. Does anyone have any advice as to how this is setup?

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

Sql difference between localhost and (LocalDb)/MSSQLLocalDB

I noticed I used a connection string to migrate an Identity Database. No matter what I did I looked and looked but I couldn't find the database. So I re-evaluated my connection strings and noticed they're not that similar:
var connectionString = #"Server=localhost;Database=MyDatabase;Trusted_Connection=True;"
var connectionString = #"Data Source=(LocalDb)\MSSQLLocalDB;database=gritzy.IdentityServer4.dbo;trusted_connection=yes;";
One just specified the server as localhost, and the other doesn't even specify a server at all.
I was under the impression that localhost would just use the default MSSQLSERVER Instance name.
What is the difference between the Data Source, and server?
"Data Source" and "Server" are synonyms, so no difference.
localhost specifies the default SQL Server instance on your machine.
(LocalDb)\MSSQLLocalDB is the default LocalDB instance on your machine
If you install both SQLExpress(SqlLocalDB) and SqlServer you are in for a big confusion.
You may have seen connection string like this:
sqlserver:
var connectionString = #"Server=localhost;Database=MyDatabase;Trusted_Connection=True;"
sqlExpress:
var connectionString = #"Data Source=(LocalDb)\MSSQLLocalDB;database=gritzy.IdentityServer4.dbo;trusted_connection=yes;";
The 1st is the connection string you use to connect to the SQLServer, which asked you to specify the root folder when you installed SqlServer. The default is "C:\Program Files\Microsoft SqlServer" but you can put it in wherever you want. For example, if I specify the root to be "C:\Source\DB" then it is "C:\source\DB\MSSQL15.MSSQLLOCALDB\MSSQL\DATA". The point here is a SQLServer instance is machine-scoped. One instance per server/machine.
The 2nd is the connection string for SQLExpress, and the DB files are usually stored in the AppData foldeer for each user, such as "C:\Users\\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB". Note that each user logged in to the same machine can have his own data file because each user has his own appData folder.
If you have both SQLExpress/SqlLocalDB and SqlServer installed, bring up SSMS (management studio) you can connect to both at the same time and you will notice they have different databases.

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

How to write connection string to connect from classic ASP page to SQL 2008 R2?

I connect from classic ASP page to SQL SERVER 2008 R2 with following connection string
"Data Source=(local);Initial Catalog=my_db;Persist Security Info=True;User ID=my_user;Password=my_pass;"
but I get error
Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I verified that my_db exists!, so what is wrong in my connection string?
The (local) name you've given for your data source is invalid. It should be an IP address, server name or server/instance name of your SQL installation.
UPDATE
If the SQL server is on the webserver you can use . to specify local. Here's an example connection string from one of my classic ASP projects:
"Provider=SQLNCLI10;Server=.;Database=my_db;Uid=user;Pwd=pass;"
You need to specify an oledb provider to prevent it trying to default to ODBC (and failing)
Provider=SQLNCLI10;Data Source=(local);Initial Catalog=my_db;User ID=XXX;Password=YYY;
(You also need the 2008 SQL Client OleDb provider installed on the machine)
try to change (local) with : 127.0.0.1
Try:
Provider=SQLNCLI10;Server=(local);Database=my_db;Uid=my_user; Pwd=my_pass;
If in doubt, check connectionstrings.com.

Resources