I am writing this blog to point out this scenario when you are trying to use LocalDB in a website and deploy on the IIS server. Most of the time we use SQL Express as a light weight version of SQL Server. Here is LocalDB so what is it?
What is LocalDB?
Let’s first talk about LocalDB. SQL Server Express LocalDB is a lightweight version of SQL Server that you can think of in many of the same ways as a SQL Server database. SQL Server Express LocalDB runs in user mode and you can install it more quickly since it requires no configuration. LocalDB is created specifically for developers and It is very easy to install and requires no management. It offers the same T-SQL language, programming surface and client-side providers as the regular SQL Server Express. Developers don’t want to concentrate on the management part of the SQL server rather they just want to concentrate on programming part and business logic. If there is no need of other features of full version of SQL Server, you can continue using SQL Server or LocalDB in the production environment as well.
We can connect to localDB by specifying (localdb)\v11.0 in server name. Here is how it would look in Sql Server Management Studio after connecting to the LocalDB database.
Now there is a confusion. What is difference between SQL Express and SQL Express LocalDB? LocalDB is not a replacement for SQL Express rather it is just an addition to SQL Server Express lineup. While LocalDB is meant for developers, SQL Server Express is still a free SQL Server edition and is easily upgradeable to higher SQL Server editions.
If we talk about the differences at the core level than as such there is no difference. LocalDB is yet another instance of the same SQL server executable sqlservr.exe and it can be found at following path C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\ . This path will be different depending on the version of the SQL Express installed.
You can think of that localDB is launched by running following command. We can used same command to run the sqlservr.exe as a different instance name like “SQLExpress” or “localDB” or “GauravDB” etc. “–m” switch makes sure that this instance in running in single user mode. For more understanding refer to this articleHow to: Start an Instance of SQL Server (sqlservr.exe)
sqlservr.exe -m -s LocalDB
For using LocalDB in ASP.NET application, we just need to specify the core connection string specifying ServerName,DatabaseName,UserNameand password. I have following connection string which i used in my application. For clearing the difference between LocalDB and SQLExpress,I have also left SQL Express connection string as commented as well. So we can see that there is as such no difference in both the connectionstrings except the instance name.
<connectionStrings><add name="LocalDBConnection" connectionString="Data Source=(LocalDB)\V11.0;Initial Catalog=SampleLocalDB;Integrated Security=SSPI;AttachDbFilename=|DataDirectory|\SampleLocalDB.mdf" providerName="System.Data.SqlClient" /><!-- <add name="SQLExpressConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SampleLocalDB;Integrated Security=True;AttachDbFilename=|DataDirectory|\SampleLocalDB.mdf;" providerName="System.Data.SqlClient"/>--></connectionStrings>
Deploying LocalDB on IIS Server
We can use simply localDB in our web application just like any other database but when we deploy our application on the IIS Server, we may see following error message when using LocalDB on IIS web server.
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: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
)
Actually, we see above error when application is not able to find the database i.e. database is not reachable. So what is happening here? You would have noticed that while discussing about LocalDB database, I said LocalDB runs in user mode and here is the key in this statement. Now if you will check Windows application event logs, we can see following logs there.
Windows API call SHGetKnownFolderPath returned error code: 5. Windows system error message is: Access is denied.
Reported at line: 401.
Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.
Second event log is self explanatory that we require to load the profile settings for the IIS worker process. Now only setting loadUserProfile="true" will not help and we will also require to set setProfileEnvironment="true". So our application pool settings should look like following.
<add name="DefaultAppPool"><processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" /></add>If you need more details about this issue you would like to go through these blogs which provides much more details about this particular issue.
http://blogs.msdn.com/b/sqlexpress/archive/2011/12/09/using-localdb-with-full-iis-part-1-user-profile.aspx
http://blogs.msdn.com/b/sqlexpress/archive/2011/12/09/using-localdb-with-full-iis-part-2-instance-ownership.aspx
Here are some other related blogs which can help you in understanding more about LocalDB.Local Data Overview http://msdn.microsoft.com/en-us/library/ms233817.aspx
SQL Server 2012 Express LocalDBhttp://technet.microsoft.com/en-us/library/hh510202.aspx
Introducing LocalDB, an improved SQL Expresshttp://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx
LocalDB: Where is My Database? http://blogs.msdn.com/b/sqlexpress/archive/2011/10/28/localdb-where-is-my-database.aspx
Hope it helps. Please let me know the feedback.
Thanks