C# 2 versions of same MDF file - c#

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.

Related

C# Visual Studio: |DataDirectory| keyword in connection string conflicts mdf files

Working on a windows form application project with 2 developers. Sharing source codes through tfs, have replaced the data directory path by |DataDirectory| keyword as the data directories are different in each PC. (C:\Users\username\Documents\Visual Studio 20XX\Projects\solution folder\solution folder)
Have used this relative path in connection string as
class ConnectionManager
{
public static SqlConnection dbcon()
{
string connectionString = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\systemdb.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
return con;
}
}
The problem is when the C# application sends data into SQL Server database, it shows that data has been added into the database, but actually the database is not updated.
Found out that the connection string conflicts the .mdf file located in the root folder with the .mdf located in bin\debug. SELECT queries work fine when using |DataDirectory|. INSERT query does not work when using |DataDirectory|, but works fine when using the actual directory path instead of |DataDirectory|.
How to solve this problem? Requirements are:
Should not need to modify directory paths each time after getting the latest version from tfs, needs something like |DataDirectory| to remain always.
Connection string should not have a conflict with the .mdf files in root folder and the bin\debug folder.
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. systemdb)
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=systemdb;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;

How to create a setup file for a project that is connected to SQL database?

As mentioned in the title of the question; I have a desktop application which is visual studio project that is connected to SQL database. I want to create an exe file (setup file) for this project, how can I attach the database with it?
Thanks.
Same problem with solution: Quick deployment of Visual Studio 2010 app with SQL database
If it's SQL server, I'd suggest scripting the database to a file, and use a custom action to call osql with that script against a database server specified in a variable.
Add the MDF to your project as a content file. The installer will copy content files to the install folder, so then it's just a problem of correctly using that database in the application. SMO might be able to help out there.
Make your database during Setup only, by Running Scripts for creation and intialization if needed
Use custom Scripts, and Custom Dialog Box to Achieve this.
You can use xcopy deployment with SQL Server Express databases. From the link mentioned:
To make your application work with the Xcopy deployment feature of SQL
Server Express, you must make sure that the connection string you use
in your application contains the appropriate parameters:
Use the data source parameter, but change the computer name to either
a period (.) or (local). You must also specify the name of the
instance, unless you are sure that SQL Server Express will always be
installed on an unnamed instance.
Use the initial catalog or database parameter, but do not set the
parameter to a value.
Add the AttachDBFileName parameter and set it to the name and path of
the .mdf file. Attachdbfilename is a SqlClient connection string
option that enables attaching databases at runtime and autogenerates
database name. The DataDirectory keyword lets you specify the relative
path of a database file. Attachdbfilenamealso helps with database
portability. For more information about Attachdbfilename, see the
Visual Studio 2005 documentation.
The following connection string will attach the MyDb.mdf database
file, which is in the same folder as the application executable, to
the SQL Server Express instance running on the local computer.
#"Data Source='.\SQLExpress'; Initial Catalog=; Integrated
Security=true; AttachDBFileName='" |DataDirectory| +
#"\MyDb.mdf'"

Where is the SQL Server Compact file?

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

Categories