Sql difference between localhost and (LocalDb)/MSSQLLocalDB - sql-server

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.

Related

SQL proxy/alias for SQL Server Express

I can't find what I need or I don't know what to search.Because we are a large team with different setup in our locals, if is possible somehow to enforce connection strings to be persistent across our locals.
Example: In config files
<connectionStrings>
<add name="SomeEntities"
connectionString="data source=.\SQLEXPRESS;initial catalog=tests;...rest of it..."/>
</connectionStrings>
As in my case, because I installed SQL DEVELOPER (version is irrelevant) my local connection will be "data source=.\;initial catalog=tests;...rest of it..."
Is it possible to set a proxy in SQL Server Configuration Manager for
<SQLSERVERNAME>\SQLEXPRESS to point on my local <SQLSERVERNAME>\
I'm not looking to install SQL Server Express, I want to setup a PROXY that
will help the team to use SQLEXPRESS connection string to work on any
installed SQL Server versions.
UPDATE:
Based on comments and responses, it's possible.
Prerequisites:
TCP/IP protocol must be enable for the instance (in my case port 1433)
To check go in CMD => netstat -an |find "1433"
Setup in SQL SERVER Configuration Manger:
Thank you for the help!
I am not sure if it is the same case that experimented, but in the past, we had a scenario where every developer was using their own name of MS SQL Server instance, we decided to set a standard name in the connectionstring and everyone has configured an alias inside SQL Server Configuration Manager, the steps are:
Enter MS SQL Server Configuration Manager.
Go to SQL Native Client XX Configuration and click in Aliases
Create a new Alias, here you should name it with the standard name that you have registered inside your connection string, inside the Protocol set TCP/IP and later in Server section, you should set up the current name of your local instance.
After that, you have completed the previous step you should be able to connect your local instance through the name that you have defined in the alias.
What are you talking about is called alias:
Create or Delete a Server Alias for Use by a Client
You can create an alias with name SQLSERVERNAME\SQLEXPRESS which will point to localhost and vice versa. Alias should be created on client machine.

How to connect from SSMS to SQL Server database with connectionString="Data Source=ABC..." (instead of localhost)

Help me please to understand, what does it mean "ABC" in:
connectionString="Data Source=ABC;Initial Catalog=xyz; User ID=***; Password=***;"
How to connect to this SQL Server database using SSMS from another Windows computer? It's no trouble for me to connect to other databases with "Data Source=localhost", but "Data Source=ABC" puzzles me.
Data Source represents the instance name. If SQL Server is installed as default instance, the hostname alone is enough. If it is installed as a named instance, hostname\instancename should be provided.
If instance is running using a different port that the default one (1433), the port number should be provided.
More details can be found here and here.

SQL Server Connection issues

I have 2 SQL Server instances installed on my Windows 7 :
First one : SQLEXPRESS - no specific database
The other one : MSSQLSERVER (classical) - several development databases...
Let's say my computer's name is SERVERNAME
Using connection string SERVERNAME\SQLEXPRESS is ok.
Using connection string . is ok and connects to MSSQLSERVER instance
Using connection string SERVERNAME does not work
Using connection string SERVERNAME\ does work (note the backslash).
Thus, until yesterday the SERVERNAME connection was working fine.
And everybody's telling me that the SERVERNAME\ connection is a abnormal one.
I've tried to connect with Windows authentication and SQL Server authentication, and cannot connect through SERVERNAME as my server name.
What should I do to recover previous functionality ?
Thanks in advance.
Regards.
You should always use SERVER_NAME\INSTANCE_NAME. In you above described scenario when you use . which means localhost it connect you the the sql server default instance name. but is it really safe to let sql server decide which instance to connect to? obviously not.
Let your connection string decide which server and what instance to connect to by using full path i.e ServerName\InstanceName. and do not worry much about other issue. as they say Keep it simple :)

can't connect to the SQL database

I created a database in SQL Server Management Studio and I called it SalonDatabase. I tried to connect to the database in VS C#. For this I created two connectionString and only one of them work so I would like to know why.
This one works:
string connString = #"Data Source=.\SQLEXPRESS;initial catalog=SalonDatabase;Integrated Security=SSPI";
However, the following does not work even though I know for a fact that the file is located in that directory
string connString = #"Data Source=.\SQLEXPRESS;
Initial Catalog=C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF;Integrated Security=SSPI";
The error I'm getting is :
Cannot open database "C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF" requested by the login. The login failed. Login failed for user 'USER-PC\USER'
Now I logged in as administrator in my computer. I also read about the user instances in SQL server but I don't see that is the reason why the second connection fails.
The database is created using SQL Server Management Studio so do not use AttachDBFileName. You may use Server Explorer (from View Menu) tool of Visual Studio to obtain the connection string.
Add connection From Server-Explorer + Right mouse click on Data Connections + Add Connection + Select Microsoft Sql Server Data Source (change).
After adding connection, right mouse click to select/open properties windows.
Data Source=.\sqlexpress;Initial Catalog=SalonDatabase;Integrated Security=True
Modify your connection to 'attach' your mdf. Given that your .mdf path has spaces, be sure to enclose it in single quotes.
Server=.\SQLExpress;
AttachDbFilename='C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF';
Database=dbname;
Integrated Security=SSPI;
Trusted_Connection=Yes;
Initial Catalog is used to set the Default Database name when we Connect to a Server.
when you are passing the complete path of the database file the connection String makes a search for a database with name C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF
which actually does not exists therefore it fails.
you can use the initial approach and if you want to use full path then AttachDbFilename can be used as suggested by P.Campbell
By default, when working with SQL Server (any edition), the server part handles all the file-related chores for you. You just create a new database (or attach an existing one) and give it a logical database name and then you refer to that database solely via this database name - you don't care (and don't need to) about the actual files that make up your database.
That's why your first connection string works - you're telling SQL Server (Express) that you want to work with a database called SalonDatabase and SQL Server knows which files this concerns.
Microsoft introduced this notion of being able to "dynamically" create a user-instance of SQL Server Express and attaching a physical .MDF file (instead of referring to a database by logical name) as a developer tool to make things easier - but quite frankly, in my opinion, it's a rather messy concept and has lots of drawbacks.
In this scenario, you basically tell SQL Server in your connection string (using AttachDbFileName=salondatabase.mdf and with User Instance=True) that it should start a separate instance of SQL Server Express for your user context, and automagically attach that physical MDF file specified as its database, and provide you access to that.
As mentioned by another comment, it's a deprecated feature - SQL Server 2012 will bring a new, hopefully slicker concept of local databases. So in my opinion, you should try to avoid using this feature - it's more trouble than anything else.

Access Visual Studio 2008 server-based database from VB 6 project

Is it possible to access a Visual Studio 2008-created server-based database from a VB-6 application? If not, what is the best way to share a database between a VB-6 application and a C# application?
If you are referring to the SQL Server Express database that comes with Visual Studio 2008, you can access that with plain old ADO.
The connection strings for doing this are at http://connectionstrings.com/sql-server-2005. The most appropriate one is probably this one:
Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;
For SQL Server Express (which is what comes with VS2008), don't miss the server name syntax Servername\SQLEXPRESS where you substitute Servername with the name of the computer where the SQL Server 2005 Express installation resides.
The reference for programming against a SQL Server database using ADO is here:
http://msdn.microsoft.com/en-us/library/aa905875(SQL.80).aspx
Example for connecting to the SQL Server database is below (untested). Note that this one uses SQL Server authentication rather than Windows Authentication. Check your connection strings on http://www.connectionstrings.com. If SQL Native Client doesn't work, try one of the others.
' Initialize variables.
Dim cn As New ADODB.Connection
Dim connectionString As String
' Specify the OLE DB provider.
cn.Provider = "{SQL Native Client}"
' Specify connection string on Open method.
connectionString = Server=myMachineName\SQLEXPRESS;Database=myDataBase; _
Uid=myUsername;Pwd=myPassword;
cn.Open connectionString
I just added a "Service-based database" to a project. I go the following in my app.config, in the configuratonStrings element:
<add name="SerializeToSql.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
I don't know if ADO has user instance support. if it does, then you may be able to create
an equivalent connection string for ADO to use. Otherwise, you'll need to just create a "normal" database in SQL Server Express. You should be able to use that from ADO.
In order for other machines to see your SQL Express instance, you need to make sure it allows remote connections, the SQL Browser service is turned on, and is allowed through the firewall.
Fire up the SQL Server 2005 Surface Area Configuration tool: All Programs->Microsoft SQL Server 2005->Configuration Tools->SQL Server Surface Area Configuration.
Click Surface Area Configuration for Services and Connections
Click the 'Remote Connections' node and select 'Local and remote connections'. If you want to refer to the server as ServerName\MSSQLSERVER, you'll have to user named pips or TCP/IP and named pipes.
Click the SQL Server Browser node. Change the Startup type to Automatic and start the service.
For the firewall, make sure you make an exception for the SQL Server program (C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\sqlservr.exe) and for the SQL Server Browser program (c:\Program Files\Microsoft SQL Server\90\Shared\sqlbrowser.exe)

Resources