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.
Related
I am working on a small windows forms program that reads data from a local database, which I have created by following this guide.
I have populated these tables with data using the Designer in Visual Studio, and there is no ability (nor will there ever be) for the program to make changes to this database at run-time, as they represent static, known data -- I could get identical results if I hard-coded the instantiation of each corresponding table row object in a constructor somewhere.
When I have Visual Studio build my solution, it generates two files -- the .exe that opens the Form, and a .mdf file with the database tables.
Two related questions -- does it even make sense to use a database with this kind of read-only data? And if so, is there a way to combine the .MDF file into the .exe? Again, there is zero need to ever modify the data, so I wouldn't think that the fact that you can't modify .exes need prevent this.
Yes you can do this. But first you need to construct your dataset. I would create your data in SQL or similar then export it to XML along with an XSD schema.
Then in Visual Studio, add a DataSet object to your project.
Then you can talk to the DataSet object and use the XSD and XML to populate it.
https://msdn.microsoft.com/en-us/library/atchhx4f(v=vs.110).aspx
To keep this all within the .EXE, embed your XML data into a Resource file, then access it via the Resources static class.
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.
I have a Microsoft SQL database that is currently connected to a winforms C# application, it works fine on the single computer, but i would like it to be usable on a CD for any user.
I tried putting it in as a localDb but for some reason the database is duplicated and put into the bin folder, it causes multiple issues in recording data, for instances i save user ID 5 it saves in bin but never makes changes to the real database. Then next i go to create it, the user ID changes to 7 with user 6 not visible in either two databases (yes it is auto incremented by 1)
Any suggestions or best methods on making a database useable and readable via CD if the winform application is also on the CD
I have not tried this my self, but according to the documentation SQLite supports read-only databases.
If the file is read-only (due to permission bits or because it is located on read-only media like a CD-ROM) then SQLite opens the database for reading only. The entire SQL database is stored in a single file on the disk. But additional temporary files may be created during the execution of an SQL command in order to store the database rollback journal or temporary and intermediate results of a query.
see https://www.sqlite.org/c_interface.html
.NET SQLite providers are available here:
https://github.com/praeclarum/sqlite-net
http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki
I'm Creating Win Form application ,I'm adding a Empty Sqlite Database file having Tables in it as embedded data source. on run time i extract Database file into application path and INSERT THE VALUES into the TABLE of that Database file.
On Closing the application again i have to update or replace Database File into Executable.
Is it possible ,if so how to do that.
I'm not sure if I understand the question correctly. If you are trying to re-write to the same exe you are running this is NOT possible. Windows locks code files that are in use so that they can't change. Additionally, it is not advisable either, code and data should be separate.
If you are trying to update another resources executable (that is not currently running), I don't know how to do that programatically (See this thread here for more info How do I replace embedded resources in a .NET assembly programmatically?) but if your program has access to the Visual Studio Compiler tools (which it probably doesn't) you can disassemble and reassemble the executable. See here: http://fortheloveofcode.wordpress.com/2007/09/24/change-resources-inside-assembly/.
Why not just store it in application folder? Or maybe user's AppData if you don't want to show it?
I have a C# winform application that accesses data from an MS Access database. This means my applications requires at least 2 files, the .exe file and the .accdb file. Is it possible to include the database in the .exe file, so my solution consists of a single file (the same way you would include an image in the project resources)? If it is possible, are they any major reasons why it shouldn't be done and how would you access the data from code? The project is a only a little one for personal use so if performance is hit it doesn't matter too much.
thanks in advance
It can be done. Simply add it to your project as you would add any other file (right click project -> Add -> Existing Item), then cancel all the dialogs that will popup offering you to handle it for you, then right click your database from your project explorer, go to properties and select Build Action: Embedded Resource.
Then use the method below to dump your database into a temporary file, which you can create by calling Path.GetTempFileName.
internal void CreateBlankDatabase(string destFile)
{
using (Stream source = new MemoryStream(Properties.Resources.MyEmbeddedDatabase))
using (Stream target = File.Open(destFile, FileMode.Truncate))
{
source.CopyTo(target);
}
}
(Note that MyEmbeddedDatabase would be your embedded database name). Then use your temporary file name in your connection string. Make sure you delete your temporary file after you're done. Also, as other said, you won't be able to modify and save any data.
No it shouldn't be done. How would you send someone and update to the .exe file without them losing their data? Keep it separate.
You need to have a way to manage how your applications installs and the file location in your connection string(s). There could be a \Data subfolder in your app folder with the .accdb file(s) in it.
You probably can't achieve what you want with an access database as an embedded resource, but you effectively get the same result by wrapping all your files in another executable app.
When you run the wrapper application, it extracts the "main" C# app, database file, and an updater app (more on this below) to the temporary files folder and runs the main app.
When the main app is closed, it runs the updater app, passing in the paths to the database file and original wrapper application. The updater app updates the wrapper application file with the changed database file. It then finally deletes the database main app and database file from the temp folder. Unfortunately, the updater app can't delete itself, but you could work around that by adding a command to the runonce section of the registry to delete the updater app on the next reboot.
Instead of figuring out how to extract and insert embedded resources, consider having the wrapper application as a compressed, self-extracting executable (like a self-extracting zip or rar file). Here's a codeproject article that describes how to turn a .Net app into a compressed, self extracting exe.
Access requires to be able to read and write to the file. The OS will lock the exe when it is run so that it can't be changed while in use. This along will cause it to not work, not to mention that Access simple wouldn't be able to read the exe as it is expecting a different file format.