I'm trying to create a recipe book in C# where I can easily search for what recipes I can make with what ingredients are on hand. This is mostly a learning exercise for me at this point.
Right now, I'm trying to test updating the tables in my database. It seems to work during one instance of the code, but the changes are gone the next time I run it.
So I have a table in my database like this:
I create a small list of example recipes from a json file
var js = new JsonImporter() { SourceDirectory = Path.Combine(Directory.GetCurrentDirectory(), "test_data") };
List<Recipe> recipes = js.ImportData<Recipe>("test_recipes.json");
Connect to my table and InsertAllOnSubmit:
DataContext db = new DataContext(connectionString);
Table<Recipe> db_recipes = db.GetTable<Recipe>();
db_recipes.InsertAllOnSubmit(recipes);
Show how many entries are in the table, submit the changes, refresh the number of entries, and show how many are in the table again:
MessageBox.Show(db_recipes.Count().ToString());
db.SubmitChanges();
db_recipes = db.GetTable<Recipe>();
MessageBox.Show(db_recipes.Count().ToString());
The first messagebox shows 1 (I have one row in the table that I entered manually via the Server Explorer), and the second message says 3. This is what I expect, since I am adding 2 entries.
However, once I run the code again, the message boxes again say 1 and 3, instead of always incrementing by 2. So it seems to me like the database is not saving. Can anybody help me figure out what's going on here?
You never said what database you use but I think it highly likely to be file based, either Access or SQL Server (in "attach the mdf file at runtime" mode)
Often with file based databases we end up setting them up so that there is a version of the db file in the project folder alongside the source code. This is copied to the output directory every time the project is built and the output directory contains the exe and the db. The exe modifies the db in the output folder. The next time it is built / run the blank db from the source code folder is copied over the top of the db that was modified in the output folder
Run your project, get the exe to modify the db, stop the exe and then search the whole project folder (using windows file search) or even the whole hard disk for the name of your db file. You'll figure out what's going on by looking at the file modified dates..
If it is a file based db thing, find the db in CS Solution explorer, click it and set its "copy to output" setting to "copy if newer" so that it only wipes the output db if eg the scheme has changed
Related
I am creating a library application in Visual Studio 2010 Professional and Access 2010. I bind the Access database to Visual Studio. When I fill out the fields and click submit I can see the new record in the DataGridView, but when I close the application the new record is not saved and I have to input the record again.
Can somebody help me to know why when I input the new record through the application the record is not saved in the database?
It is a very common situation. You have your database file (the MDB or ACCDB file) listed between your project items. If you click on this file and look at the properties window you will see a property called Copy to the output directory. If this property is set to Copy Always then every time you start a debug session the database file listed in your project items is copied by VS in the output directory (usually BIN\DEBUG). Of course this copy doesn't contain the records inserted in your last debug session and you think that your previous insert has failed.
Setting this property to Copy If Newer, the mentioned behavior will happen only if you change the database schema manually.
Setting this property to Copy Never, will let you manually copy the database file.
I have a simple question. I'm new to .net and sql and I'm trying to write a small form application. I added a data source which I created in sql. I got textboxes, button to update tables in database. When I made changes in database, like adding or deleting rows from tables, these updates are getting lost if I made a little change in application code (for example entering a new line).
Probably I'm unaware of something simple. What is the cause of it?
Select the database from solution explorer, go to properties (or alt + enter) and change the Copy to Output Directory to Copy if newer (copy if you change the database) or Do not copy. If the selected option is Copy always, the database is copied each time the project is build.
In my project, I have a data-access layer that contains .dbml file(named test.dbml) used to drag and drop stored procedures and tables. Two days ago when I drag and drop the stored procedure onto my test.dbml file. Upon saving it creates another test.designer1.cs class and the old test.designer.cs class remains unchanged. I use visual studio 2010 and it was very weird for me. I've checked my rights on the folder which contains my project but all seems ok because two days earlier it was working fine and suddenly it happened. I Google it but find no solution so I decided to write this question.
At-last i've solved it myself.........and i am going to share it that what i have done. my dbml file name was test.dbml having test.dbml.layout and test.designer.cs files now the problem was when ever i drag and drop stored procedure or table it creates a new designer file with name test.desginer1.cs and when i build data-ccess layer it gives me hell of errors my brain was out at that time and with angry face i delete the test.designer.cs (original file) from the solution after that there was only one file test.designer1.cs which i renamed from test.designer1.cs to test.designer.cs and build data-access layer and i was wondered that it was build successfully. this solve my problem however there are other points as well to keep in mind
make sure your dbml files should not be read-only
you have full access to write file on disk (means check your rights on directory where your solution is placed)
I finished my c# Click once application, so I published it.
The problem with it is, that I'am using a small SQLite database, for keeping settings and a lot of other small things, which will get overwritten by the new SQLite database, every time I update my application.
There are settings in the database like: Remember password and Remember username, there is also a counter, that counts how many times the application has been started. All of that will be lost and the user will have to fill in his username and password again, even if he checked the boxes to remember, because the database will be overwritten.
Question: How do I prevent to SQLite Database from getting overwritten in my Click Once application?
I used the following link to solve my problem:
http://robindotnet.wordpress.com/2009/08/19/where-do-i-put-my-data-to-keep-it-safe-from-clickonce-updates/
This article explains how I can create my Database in the roaming folder of AppData, create a folder there called like your company name or the name of your application and put the Database in that folder.
You should have "Template" database which will always override and created database with data which will not override after installing of new version of application.
The database with data should be created if it doesn't exist at startup of application.
I am trying to learn Monotouch and have an SQLite db with information.
Using the examples here I can open, fill and query the database (which is set as "Content" in my project), but when I rerun the program, no data exists in the database again.
I am guessing that somehow I have to save the database file to the Documents directory after changing it, but I can't quite figure out how to do that. Do I have to open the file with File.Open, then open another one in Documents and write to that one line by line? That doesn't seem efficient or correct.
Does anyone have any examples of this?
What is the "Copy to Output Directory" set to for you db file? I'd guess the most likely problem is that every time you build in the IDE, the blank db is overwriting the modified db from your last session.