Connect to LocalDB (Visual Studio 2013) from SSMS 2014 - asp.net

I'm confused about how to connect to LocalDb from SQL Server 2014 Management Studio. (I also have SQL Standard Edition installed on localhost.)
I have a connection in my ASP.NET application like this:
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\BillTest.mdf;Initial Catalog=BillTest;Integrated Security=True"
providerName="System.Data.SqlClient" />
When I go into SSMS 2014 and connect to (LocalDb)\v11.0 I can connect and I see a database like aspnet-BillTest-2015mmddhhmmss but I can't open it. I get an error like "the database ... is not accessible."
When I connect to (LocalDb)\mssqllocaldb I can connect, and I see a DB like BillContext and there are my tables. I verified this is the one the app is connected to, by inserting new rows and seeing them show up in my app.
Can you explain the discrepancy in connect strings:
how is my ASP.NET application using (LocalDb)\mssqllocaldb when the connect string in Web.config is (LocalDb)\v11.0? What is actually on \v11.0?
And why does (LocalDb)\mssqllocaldb point to a SQL version (12.0.2000) that is neither 11.0 nor my SQL Server 2014 installation (localhost = 12.0.4100.1)?
why is the DB name "BillContext" instead of "BillTest"?

It's because of the LocalDB Server Version and SSMS version. Till Visual Studio 2013 you were supposed to use version specific connection strings (e.g (LocalDb\v11.0) from Visual Studio 2015 onwards you are supposed to use version independent connection strings (LocalDB\MSSQLDB) in order to work properly. Check this link for reference.
Version independent local DB in Visual Studio 2015 - Bill Wagner

why does (LocalDb)\v11.0 work from my ASP.NET application but not within SSMS?
It sounds like (LocalDb)\v11.0 is accessible from both from what you describe, it's just that the database itself isn't accessible. I'd check if the file still exists or if something else has locked the file.
why does (LocalDb)\mssqllocaldb point to a version (12.0.2000) that is neither 11.0 nor my SQL Server 2014 installation (localhost = 12.0.4100.1)?
Assembly version and file version are different. See this answer
why is the DB name "BillContext" instead of "BillTest"?
You're probably not providing a connection name to the EF context (e.g. new BillContext("DefaultConnection")) so it's falling back to the default, which is a LocalDb database named after your context.
https://msdn.microsoft.com/en-us/data/jj592674
https://msdn.microsoft.com/en-us/data/jj591621.aspx

Related

Database is removed from Microsoft SQL Server, Why?

I created a new Empty website in Visual studio Express 2013.
I created an ASP.NET folder: App_Data.
Then I went to SQL Server Management Studio. I created a new Database in which I provided the path to the App_Data of the website that I created.
I named the database "FacebookData"
Then I went to Visual studio; I found the database in the App_Data folder and also in Server Explorer.
I went to Web.config, And under I added
<connectionStrings>
<add name="DataConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FacebookData.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I click Build -> Rebuild Solution
When I close SQL Server Management Studio, and reopen it again.
The Database named "FacebookData" is missed from the SQL Server Management Studio Databases list!!!.
I still find it in the Visual Studio in App_Data folder and Server Express.
If I close and open the SQL Server Management Studio before adding the to the Web.config. The Database is still exist in the SQL Server Management Studio. which makes me feel that there is something wrong in the connection string.
Any clues?
Your connection string says that database should be "attached" to SQL server when application is run. To have it permanently living in SQl server you should just use SQL server management studio to attach it manually, that way it stays attached. You also need to change you connection string to
"Server=your_sql_server_instance;Database=FacebookData;Integrated Security=True"

SQL Server mdf file cannot be connected by both ASP.NET application and SQL Server Management Studio

I have a simple ASP.NET website application. Using the new Entity Framework model-first approach, I created a database (.mdf file). However, whenever the application is running, SSMS cannot connect to the database (error 5123 or 5120), and vice versa.
Is this normal? Is there a way to get around this?
I have tried to run SSMS as Administrator, but that did not seem to help.
I am using VS 2012 Professional and SQL Server 2012 Express.
Here is the connection string of the app.
<connectionStrings>
<add name="MarketingContainer"
connectionString="metadata=res://*/App_Code.Marketing.csdl|res://*/App_Code.Marketing.ssdl|res://*/App_Code.Marketing.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012ADV\MSSQL\DATA\MarketingDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient"/>
</connectionStrings>
When you use AttachDbFileName you place a lock to the file and use as single user mode and only one connection can use the database.
For use the database between Visual Studio and SQL Server you must:
1. Attach the database (not the file) to SQL Server
2. Use the connection string to point to SQL Server
Max answer is in principle correct. I have now implemented his answer, using this connection string
add name="MarketingContainer" connectionString="metadata=res://*/App_Code.Marketing.csdl|res://*/App_Code.Marketing.ssdl|res://*/App_Code.Marketing.msl;provider=System.Data.SqlClient;provider connection string="data source=SQLSERVERNAME;initial catalog=DATABASENAME;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"
I should note that the reason that my problem arose in the first place was this. I use VS2012 to create an entity data model (edmx file). after I get the model to where I want it, I generate the database from model. at that point, the Generate Database Wizard shows up. New Connection. Change Data Source. I choose Microsoft SQL Server Database File (because I only had a mdf file at that point). If you do it this way, you end up with the application using LocalDB, and Attachdbfilename locks the file as Max said. However, what I should have done is first use SSMS to attach to the mdf file, and then rename the database as needed. and then in VS2012, Change Data Source, select Microsoft SQL Server instead. and now, both VS2012 and SSMS can access the database.

How to configure ASP.NET membership after removing SQL Express 2008 and installing SQL 2008 R2 Enterprise

As the title says, I used to have SQL Express 2008 on my machine. After creating a new Web application, Visual Studio would automatically create the aspnetdb.mdf database and add a connection string to the Web.config file.
Since installing the full version I've found, to my surprise, that it's not possible to reference a local .mdf database any more. Firstly, is that true?
With that in mind, I used aspnet_regsql.exe to create an aspnetdb.dbo database on my local server. I then used the 'Server Explorer', in VisualStudio, to attach that database to the 'Data Connections'.
The problem is, every time I create a new web application, it sticks a local SQLExpress connection string into the project template.
Is there something else I have to do to rectify this?
Thanks

Using SQL server 2008 for ASP.net 2.0 Membership provider

I tried to set-up the SQL tables for ASP .net 2.0 membership provider by clicking on ASP.net Configuration under Project.
I have completely uninstalled SQL 2005 Express and installed SQL 2008 Express instead.
It seems like that it can not connect to the database.
Does anybody know how to get this fixed so I can use 2008 instead ?
Try to configuring the database typing "aspnet_regsql" on the VS command prompt.
Then setup the database and finally change de connection string on the web.config file.
ASP.NET SQL Server Registration Tool (Aspnet_regsql.exe)
What is your SQL 2008 installation instance called? What does your connection string for the membership provider look like?
As far as I know, there is (or was) a glitch in the SQL Server 2008 Express installation that even if you chose to install as "default" instance, it would still make it a "named instance" called ".\SQLExpress" instead. Could that be the problem?
Marc
As Marc already has pointed out, it could be the problem with the instance name. This is fixed in service pack 1 for SQL Server 2008 Express. For more information and a workaround, see this Microsoft KB article.
SQL 2005 and 2008 are both configured in a fairly locked down state by default - and this means that SQL won't accept access the database other than through Shared Memory, even with Integrated Security.
You need to enable TCP/IP or Named Pipes for the Client Protocols for the server (as this is how ASP.NET talks to SQL if you've not configured a DSN to use Shared Memory.
I can't remember the exact steps in 2k8, as I've only had to do it once, but in 2k5 you had to open the SQL Server Configuration Manager, select SQL Native Client Configuration, Client Protocols, and enable TCP/IP and Named Pipes - it's a similar process for 2k8 as I recall.
The connectionStringName used in the membership Tag should be changed to the present connection string where you have the membership tables created.
If the connectionStringName is not pointing to a valid connection String name in the connectionStrings tag, the membership tables can not be used by the application.

Minimum requirements to use AspNetSqlMembershipProvider

I'm using an AspNetSqlMembershipProvider for my application.
On my development machine I recently deinstalled SQL Server 2005 Express and Management Studio Express so that I could install the full version of Management Studio. I chose not to install SQL Server 2005 because, well, I don't want to have unnecessary things running on my machine.
Now whenever my code attempts to do anything that relates to authentication I get an error of:
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)
So, what is the minimum amount of SQL Server I have to install in order for this to work?
You could get away without having any SQL Server instances installed, especially if all you're using it for is Membership, Roles and the default Profiles.
The default connection string often looks like this:
<connectionStrings>
<add name="LocalSqlServer"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
If you change your SQL connection string, to match the value of "connectionString" above, that will force ASP.NET to generate the standard ASP.NET database for you withing the "App_Data" folder of your site - the keep part is "AttachDBFilename" which tells it to open the mdf file directly rather than attach to a SQL instance.
However, you might have some difficulty connecting to that file with SQL Management Studio.
You'll probably have to change the connection string once you move the site off development and into production, to point to a full instance of SQL if you so wish, rather than stick with the file based version.
Alternatively, what I tend do is have SQL Server installed (Express or otherwise), but the services stopped unless I'm actually using them. Then I just start them up when I'm developing.
Also you can have the Express version of SQL Server installed on the same machine as theh the full version SQL Management Studio without any problems.

Resources