Monotouch and SQLite database won't retain changes - c#

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.

Related

Database seems to update during execution, then changes disappear

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

Not overwriting saved xml files in C# Window Form

I am fairly new to C# and I was wondering how to keep an XML file from being overwritten if one already exists on the install. In the application, there are two files that contain info to connect to the Database. One of them is relatively dynamic, but the other is saved at the setup. If I do a publish and try to update the application it always overwrites both files. Any thoughts?
You can check if the file exists with File.Exists(Path)
You can look here for more information:
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.exists?view=netcore-3.1
If this does not work you can try to read that file and if there is any data there just sont delete it.
I assume you are refering to configuration settings.
While designing your settings, in the designer, set scope to "User". This will bind the setting to the users local app settings, and will not be overwritten.
More info here: https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-create-a-new-setting-at-design-time

Losing database update after changes in code

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.

What is R60, R72, and MINE file?

I was building a DataAccessLayer using C# in VS 2012, and in my project, there are three files which I don't know how to deal with.
DataAccessLayer.csproj.mine
DataAccessLayer.csproj.r60
DataAccessLayer.csproj.r72
What are they? How should I deal with them?
They are files created from your repository that the code is saved in. You did an update and when you try to commit your files you will get a commit failed probably. When you diff the files, it will use the .mine .r60 .r72 files to bring up the differences between each revision (.r60, r72) and your file (.mine) and allow you to do something like Keep all changes in mine.
In order to get rid of the commit failed error messages, find the original file, update it with the correct changes, and when finished right click it and Mark as resolved and commit. This will get rid of those files.
Those files are related to version control (.mine is your file, .rX come from revision number X). You should try to merge them using your version controlling tool if you have one. If you don't have one and got those files by copying a project, you can possibly delete the .r files and rename DataAccessLayer.csproj.mine in DataAccessLayer.csproj (unless you already have DataAccessLayer.csproj - just keep it and delete the other then). Edit: And if it causes problems, try merging them manually (using a text editor for example) before keeping the final file.

.mdf file does not respond to SqlCommand.ExecuteNonQuery

I have a weird problem with my local database. I added it as a .mdf file, and created 2 tables in it.
I tested the connection and the message box states that the connection is working properly.
The problem appears when I try to insert data into that database from my C# application. I tried 2 different ways:
By using SqlCommand.ExecuteNonQuery("INSERT INTO ....")
By using .dbml file (Linq-to-SQL) datacontext
When I try to insert data, no error is thrown, but no data is inserted into the database either. What could be the problem?
I'm using C# in VS 2010 and SQL Server 2005 to make my application
most perceived problems with MDF files tend to boil down to one of:
what file have I actually opened?
is my build/run process actually copying over that file every time I run the application?
do I have gratuitous error-handling that is swallowing an exception?
Check your connection string, and look in the execution folder (not the project folder), until you are very sure which file is being opened. It should get updated after your work (make sure you dispose the connection etc properly).
Also: try fetching the data in a new data-context immediately after the insert, so see if it made it in or not.

Categories