What is the best practice when using SQLite as file in wpf app.
I have a database driven app and I want to use SQLite as a file per project. But I'm very green in programming.
Is it correct that each time the user makes a new project the app makes a new database file, holding a fixed table structure that can be used in the app?
This means that each sqlite database file has a unique name and all the dbconnections that are made in the app should correspond to read and write to that file.
Now my read and write dbmethods are spread out over a few viewmodels and how do I dynamically change
String dbConnectionString = #"Data Source =projectA.sqlite";
to
String dbConnectionString = #"Data Source =projectB.sqlite";
String dbConnectionString = #"Data Source =projectC.sqlite";
each one corresponding to the project they want to load and work on in the app.
I don't find much info about this matter on the web.
Maybe someone can explain me what the best practice is or how sqlite dB are used as files for a wpf app.
Is it correct that each time the user makes a new project the app makes a new database file,
No. This is only the case if you program your app in such a way that it picks a new filename for the database file every time.
holding a fixed table structure that can be used in the app?
Not necessarily. You can change a table structure to some extent while your app is running, and this can also depend on user input.
This means that each sqlite database file has a unique name
Only if you actively make sure it is unique.
and all the dbconnections that are made in the app should correspond to read and write to that file.
Only if you specify exactly the aforementioned unique filename for the connection. You can also open connections to any other more permanent/non-unique SQLite file from your app (and even to several at a time).
Now my read and write dbmethods are spread out over a few viewmodels and how do I dynamically change (...)
Provide a place that is somewhat global in your app (either a static class, or a central configuration class an instance of which is passed around to each of your modules) that returns the filename from a read-only property. In all of your DB-related methods, use the information from that property rather than a hard-coded string.
Related
This question already has answers here:
Preserve data between application executions
(4 answers)
Closed 4 years ago.
I want to use a data saving feature in several of my simple winform apps. Is there a way to simply save a value of variables somewhere (Database, Textfile) and load it after each start of the app?
And what is the simplest way to do so?
There are too many ways to save your data after closing, some of them :
Settings
to add it programmatically, you can do :
System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("Sample1");
Properties.Settings.Default["Sample1"] = SomeStringValue;
Properties.Settings.Default.Save();
Database
You can save your data in database tables if they are large
XML File
if you have a medium embedded data the xml file is the way to go.
Finally, its all depends on what data you want to store.
You can use one of the options below
Use the settings that come with visual studio to save your variable and load them back when you start the app the next time.Like below
Go to your project properties and go to settings then add a new settings . You can add as many settings as you want using the settings designer. They can be of different variable types (string, color,into,etc)
To change a setting from code, use
//we created a setting named 'mysetting' of type to string
Properties.Settings.Default.mysetting = "test string";
//Save the setting
Properties.Settings.Default.Save();
//To get the value of a setting, use
string mystr = Properties.Settings. Default.mysetting;
You can simply write the value of the variable to a text file and read it back on startup.
One of the easiest solution for your would be to use the Application Settings which inside Visual Studio, you can easily do much of the work in the design time as well.
Starting with the .NET Framework 2.0, you can create and access values
that are persisted between application execution sessions. These
values are called settings. Settings can represent user preferences,
or valuable information the application needs to use. For example, you
might create a series of settings that store user preferences for the
color scheme of an application. Or you might store the connection
string that specifies a database that your application uses. Settings
allow you to both persist information that is critical to the
application outside the code, and to create profiles that store the
preferences of individual users.
Read the documentation for examples.
You can use System.Configuration.Settings which will save your configs into YourProgram.exe.config in xml format, but YOU WILL NEED TO CALL Save() MANUALLY TO SAVE THE MODIFIED CONFIG. In .Net programming, I consider it very convenient.
You can also define your own config file, a simple approach can be using json.
Another approach is to use a local database like sqlite(recommended) or access(less recommended) an define a table called 'config' to save all your config fields.
The least recommended approach is to save your config into windows registery.WHICH WILL MAKE YOUR USER VERY ANNOYED.
I am now involved in a small c# project on winform (just 2-3 select statements),
and the project need to connect to mssql.
I know there are many ways to connect DB.
but why people use .ini or xml file to connect DB?
Is not good insert connect statement(server, id, pw ...) to class?
It is easier to use Configuration file (.ini, .xml) if you want to change connection string later. If you put your data in your code, everytime you change it, you must re-compile your code.
I'm new to Android.
I am using Xamarin on Visual Studio and coding in C#.
I've seen samples of creating a database if it does not exist, but not what I'm looking for.
Does anyone have any code (C#) for using existing sqlite database already in Assets folder, instead of creating new database on Android device?
You have to copy the database file from your Assets folder to somewhere on the device, and then use that in your application. Note that you only have to do this once, when your application first starts up.
Opening an existing SQLite database differs very little from creating a new one.
Assuming you are using sqlite-net (which you should),
all you need to do is create a class that inherits from SQLiteConnection.
public class CustomerDatabase : SQLiteConnection
{
public CustomerDatabase(string filename) : base(filename, true)
{
//This will open the database file specified by filename
}
}
If the specified filename refers to an existing database, the database will be openened.If it refers to a file that does not exist, an attempt will be made to create a new database.
At this point I'm going to assume you have 2 issues:
You want to open and manipulate the database even if you do not havethe model defined as part of your application. Which means your application hasno knowledge of what the database actually looks like. As a result the only optionswould be to either define them or execute raw SQL queries and deal with the dynamic response.
You added an existing database to your project in the assets folder,but as far as I know there is no way to perform write operations to those files at runtime.Instead you should copy the database to the normal storage when the application is first started.
Other than that, you should be all set to go.
I need some setting of an application that will be shared among all users of the computer, but could also be changed at at run time. That seam simple, but according to the Application Settings MSDN article, it's either one or the other.
There are two types of application settings, based on scope:
Application-scoped settings can be used for information such as a URL for a Web service or a database connection string. These values are associated with the application. Therefore, users cannot change them at run time.
User-scoped settings can be used for information such as persisting the last position of a form or a font preference. Users can change these values at run time.
I could write code to edit the app.config XML file, but since it's located in the program directory, it's protected under windows 7. So this is not possible without elevating the program or playing with NTFS rights.
So I need the configuration file to be written in a common folder like System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData).
But this is a fairly common requirement!
So, I'm wondering if there a simple way of achieving this without reinventing the wheel, or if I have to write my own Setting Manager.
After reading the answers here and playing with the ConfigurationManager.Open methods I ended up with the following:
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyApp", "MyApp.config");
Configuration MyAppConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None);
The nice part is the file doesn't have to exist, and if you call Save() on it, it will create the folder and the file for you. AppSettings["key"] didn't work though so I had to use
MyAppConfig.AppSettings.Settings["key"].Value
to read and write an existing value and
MyAppConfig.AppSettings.Settings.Add("key", value)
to add a new value.
I have had a similar problem and ended up writing my own settings class. It was very basic. I created a Settings class with the properties I needed, and a SettingsManager with Save() and Load() methods that simply serialized/deserialized the object via XmlSerializer into/from a file.
Yes, it is your own code, but it is very simple code, takes less time than trying to figure out whether there is a component providing what you need and how to customize it.
The Application Settings infrastructure does not support this - only non-editable application data and user-scoped data are supported. You can easily read and write your own XML into the CommonApplicationData folders, however, instead of using the application data.
I have an app using a SQLite db, and I need the ability for the user to move the data file and point the app to where it moved to. I used the Entity Framework to create the model, and by default it puts the connection string in the App.Config file. From what I've read if I make changes to the connection string there then they won't take effect until the app is restarted. That seems a bit clunky for my use. I see how I can init my model and pass in a custom string but I'm unsure what the best practice is in where to store basic user prefrences such as this? Ini, Registry, somewhere else? I don't want the user to have to "Open" the file each time, just when it relocates and then the app will try to auto open from then on.
Have a look at Application Settings for an overview of how to create user-specific config settings which can be saved to a user.config file. The registry is more or less abandoned in favour of the new xml-based config file system.
You don't have to use the Connection String that is added to the App.Config. You can skip adding it actually, in the EDMX wizard.
You then need simply have the connection string live anywhere you choose and pass it into your ObjectContext constructor.
You can put the connection string in an external file, the registry, or wherever you choose.
It might make sense to have a static class that generates the connection string, and grabs the file location from a common source that the user can change (i.e. registry, file on disk, environment variable, etc.)
You could create a settings class and then serialise it to an xml file with a predfined name in a location that is set via the app.config file. You could then control how frequently the file was read into memory yourself. The only timeyou would need to restart the app was if the location of the settings file changed.