Where is the SQL Server Compact file? - c#

I have an ASP.NET MVC project that uses a SQL Server Compact database. I have the following connection string for my FoobarContext:
<add name="FoobarContext"
connectionString="Data Source=|DataDirectory|Foobar.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
I fired up the project and it appears to persist data as expected. However, I can't find a file named 'Foobar.sdf' anywhere. What am I missing?
Update:
It wasn't taking my connection string. Setting a breakpoint and inspecting the context, I found its ConnectionString was actually the following:
Data Source=.\\SQLEXPRESS;Initial Catalog=MvcApplication3.Infrastructure.Data.FoobarContext;Integrated Security=True;MultipleActiveResultSets=True
Mystery solved.

It definitely should be ~/App_Data by default, but you can try running AppDomain.CurrentDomain.GetData("DataDirectory") to see where ASP.Net thinks the directory exists.
If this reveals nothing of interest, you may want to make sure your windows explorer is set to show hidden files, just in case that's why you don't see the file.

It should be in a folder called App_Data off the root of your MVC project.

you should be stingy on the app folder in your project, the database file you'll find inside.
Regards

Related

Attempt to attach an auto-named database when installing my app in another pc

I recently finished building this C# Winforms app that uses a localDB connection and when installing it on another PC, I get the error as shown in the title, even though I included the SQL Server Express localDB in the prerequisites folder.
I tried all kinds of solutions like changing the method of how to build the setup file (using Advanced Installer), but it seems that the problem is always related to the constant AttachDbFilename attribute of the connection string that's not changing according to where the database is newly installed.
Here's how the connection string is defined in the App.config file:
<connectionStrings>
<add name="client"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Surface\source\repos\VisaTurbo\VisaTurbo\Client.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Please I've been stuck with this problem for 3 days now and the due date to submit the application have already been reached.
Thanks in advance
You can by example check the database file exist in %userProfile%\DBName.mdf and create a new one with https://github.com/martincostello/sqllocaldb.
But this make the connectionstrings parameters obsolete :)
You can also generate a new one with SqlConnectionStringBuilder
But if the user move the database file this will reset the application each time.
That's why in general the database in on a server, not locally stored on the user's machine.

C# 2 versions of same MDF file

I have MDF file which i use as DB and connecting to it using Linq-to-SQL.
my connection string is:
<add name="TasteTeam.Properties.Settings.TasteDBConnectionString" connectionString="Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\TasteDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
my problem is that when i run the program and adds data to a table it adds into the MDF file that in the bin folder instead of the main MDF file.
this situation causes the MDF file to be empty every time i restart myprogram instead of persisting the data.
what can i do to make both MDF and the tmp MDF file in the bin be the same file
(i already tried all the "copy to output directory" options but nothing seem to help)
The whole AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!
If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.
The real solution in my opinion would be to
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g. TasteDB)
connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:
Data Source=.\\SQLEXPRESS;Database=TasteDB;Integrated Security=True
and everything else is exactly the same as before...
Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.

Visual Studio 2013 not creating database when I select the authentication option

I tried this on two different computers but get the same issue.
I am using the latest and greatest visual studio 2013.
I create a new project (of type Web->Asp.Net application)
Choose a template type of MVC or Web API (have same issue whatever type of template I select).
Then I change the authentication type to use individual.
The project with all the authentication code is created.
Web.config has the connection string set for a .mdf file.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20131031102303.mdf;Initial Catalog=aspnet-WebApplication1-20131031102303;Integrated Security=True" providerName="System.Data.SqlClient" />
But the mdf file is missing in the app data folder. I searched the whole computer for the mdf file, but it is not created anywhere.
As a result testing my sign in and register modules using a browser gives me the Yellow Screen Of Death with the db connection being the issue.
What am I missing here?
It should be in
C:\Users\[*yourusername*]\Documents\Visual Studio 2013\Projects\WebApplication2\App_Data
This is not a real answer to my question as to why this is happening. But here is the workaround I did to make it happen. I downloaded one of their sample projects which did have the mdf file in the app_data folder. Then I got the table schema from that mdf file and created those tables in my SQL Server database. Then I changed my connection string to point to my SQL Server database. My authentication pages worked after that with this new database. But what a pain to go through.

database file not showing in app_data in asp.net mvc4 tutorial using visual studios 2010

I'm doing this tutorial on ASP.NET: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4 and I reached a stage in which I need to work with a database.
Since I'm using Windows XP, LocalDB is not an option, so I configured SQL Server Express instead.
The app is working, but I can't seem to see the MovieDBContext.mdf file in the App_Data folder.
I tried refreshing, building, running, nothing helps.
The only thing in that folder is ASPNETDB.mdf. Does this file contain what I'm looking for?
I opened it and it showed a bunch of tables related to users of the app.
My problem comes when it's time to add new fields to the movie class and coordinate it with the database, meaning I have to turn on migration.
Here are the instructions for migration:
http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table
Sadly I cannot delete the database files according to this tutorial since I don't have access to the files.
Is there a way to see the .mdf files?
my connection string is:
<add name="MovieDBContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Drivers;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient"
/>
In the order they come in your question
The only thing in that folder is ASPNETDB.mdf. Does this file contain what I'm looking for?
Probably yes, it actually depends on the name you gave to the file when it was created.
Your Connection String
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Movies.mdf;
It looks for a file named Movies.mdf in the App_Data folder whereas your file is names ASPNETDB.mdf, either rename the file to Movies.mdf or change the connection string
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;

Unable to find where my database file and connection string is located?

i downloaded a demo application from http://code.msdn.microsoft.com/Authorization-based-aa2c0185/view/SourceCode
I run it on my Visual Studio and all worked fine. There is a database ASPNETDB.MDF in its App_Data folder which is used to store the user information or retrieve information for login purpose.
Now what I want is to change this database with another database in my SQL Server.
For that there can be two ways :
Locate the connection string etc. for ASPNETDB and change them with my own database.
or add this ASPNETDB file to SQL Server by attaching. But its showing error while attaching it.
Also if I exclude App_Data folder from project...Then even it works...
Where can find connection string for DB? I want to change the DB with mine own ?
Please Help
Thanks
It's using default connection string from machine.config:
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
Go to the Web.debug.config file. There you will find comments noting how to wire up your config file. This will use config transforms so that when you build in debug mode, it will change your config file to override the machine defaults (which is what it is currently using). Just follow the comments and you should be good :)

Categories