Service-based database file difference - c#

I am creating a C# winform application that is using a .mdf database I am creating and attaching to the project via the Add New -> Service Based Database. I can add the database and and add tables just fine via Visual Studio and programatically. But when I go to view the data in Visual Studio, my data I inserted is not present. I believe this is because of the fact that the .mdf file that Visual Studio sees and reads is in the project folder where as the .mdf file the code modifies is put in the bin folder.

Please refer the MSDN link : How to: Manage Local Data Files in Your Project

Related

Adding new Service-based database to C# project in Visual Studio 2013

Lately i was building C# windows forms application with service-based database.
I was forced to make few changes to make it work, but it's still not working as it should.
I'am doing this at start:
1. Add Service-based database to project
2. Create Tables
3. Add Entity framework 6.1.2 Model to project
This creates seccond database file in Project and in Data Connection. Now if i want to show any data, I must create dgv and dataSet pointing at this newly created .mdf file. Also i need to change Copy to Output Directory to Copy if newer. Now I can see my data, but i only see empty table in Data Connection in Server Explorer. Also if I open it and refresh, all data in my application are gone.
Is there any tutorial, or something about How to add Service-based database and connect to this file using entity framework, wihout coppying it to output directory?
If someone need a solution for this case, there is one:
Create database in any program (could be visual studio)
Copy it to /bin/debug/
Put bin folder to your solution in Visual Studio
Select database and make Copy to Output - Do not copy
Edit connection string in entity framework to
attachdbfilename=|DataDirectory|\file.mdf;
Database should now appear in Server Explorer in VS. You can now remove all newly created databases (leave only this one you copy to bin/debug/)
Now you will see correct database in Data Connections, and your program will update this one. You can easily coppy your solution to any other location, and connections will not be messed up.
Btw, I think this is entity framework error or DataDirectory should point to Solution folder, not bin/debug. I know we can change DataDirectory pointing...

View local database in Server Explorer

I've created a local database in my C# Windows Application and I'm inserting data into a table in the database. My question is why can't I see the data in the table when I view the table in the server explorer? I see that there's a database in the /bin/debug folder that's getting the data but thats not the one thats available in the server explorer. Is there a setting somewhere in Visual Studio 2010 to use the project database not the debug version?
If you open the App.config you can change the data source of the connectionString to a direct link to your database file. Also, select your database in the Solution Explorer and in the Properties section change the copy to output property to "Do not copy"
Exactly i think, you have two copy of your database.one in your bin folder and another in your server explorer(with different location).when you add data into table exactly you insert them into database at your bin folder(a copy of your database).
check your address of database in your connection string,it must be the same as address of your database in server explorer.(do you use EF?)

How do I change SQLite file location when using typed dataset in C#

I used vs 2010 wizard in order to connect a SQLite database, and while using the wizard I browsed the db file from specific folder. Now I wish to change the folder but can't find how to do that.
I found the path for the file in project's properties -> Settings.settings -> Settings.Designer.cs
the attribute global::System.Configuration.DefaultSettingValueAttribute
just change the value and it works.

how do I duplicate LightSwitch DB structure in a second LightSwitch application?

Suppose I have two MS LightSwitch projects. I really like my table layout in one project and want to copy it to the other project. How is this done? What files have the table and key definitions?
The easiest way would be to publish the application, then use a copy of the published database as an attached data source in the new project.
The defintions that you ask about are stored in an lsml file (a LightSwitch-specific XML file), ApplicationDefinition.lsml for LS 2011, & Common.lsml for LS 2012. But be VERY careful if you try to edit this file in any project, it only takes ONE mistake for the whole application to no longer load.
[WARNING: NEVER DO THE FOLLOWING! IT MESSES UP YOUR PROJECT IN VISUAL STUDIO]
What I did in LS 2013 was to create tables with the same name in the new project, so they are already "known" to your project settings. Then I went to [project folder]\[project name]\[project name]\[project name].server\DataSources\ApplicationData of the project I wanted the tables from. I copied the respective files (including script files, if you did data validation, which I also created in the new project with dummy entries) and then overwrote the files with the same name in the respective ApplicationData folder of the new project, which I had created.
When I loaded the project again, Lightswitch showed the new tables in my old project.

C# Express Edition + SQL Server 2008 R2 Express; Database file being recreated on run

I created a standard C# console application in 2010 Express, used the wizards to create a simple sql express db file which gets placed in the project dir, used the wizards to generate an entity data model based on the newly created db, wrote a few lines to test out entity.
Problem is each time I run the application it recreates the DB file in whatever dir the exe is in overwriting itself everytime and completly ignoring the original db file that sits in the project dir.
Does anyone know what I am missing here ?
Not entirely sure about this but try it out.
Make sure your connection string points to the database file in the project directory.
Select the file in Visual Studio and Choose 'Copy to Output Directory' ->> Do Not Copy.
Hope it works.
you must be using some way to find that file or some path parameter within the database connection that points to the database file.
Now either you can have that parameter be generated by code or make the directory specific and make the database file copy at a specific location from the original location so that the only that particular database is accessed using the application.
For doing that you can add another key in the application config file or at some other place so that the database your application is accessing is in the project directory itself at all times.

Categories