Isolated Storage in Windows Phone stores data specific to an app. The storage space is not shared with other apps. SQLite is a light-weight database. And for mobile apps, it is common to store app specific data in SQLite. Windows Phone apps store SQLite database in Isolated Storage.
The usual way to open a connection to the SQLite database is using the path to the database file.
_conn = new SQLiteConnection(path);
SQLite database file path
When the database is available in Isolated Storage, How do we get a connection? The IsolatedStorageFile class of Windows Phone does not have a method to get the file path. We have a Microsoft way of getting the file path of a file in Isolated Storage.
string path = string.Empty; using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (var isoStream = isoStore.OpenFile ("countries.sqlite", System.IO.FileMode.Open)) { path = isoStream.Name; } } _conn = new SQLiteConnection(path);
First, we get the IsolatedStorageFile
object for the app. Use the GetUserStoreForApplication
method of IsolatedStorageFile
class. We then open the SQLite file using OpenFile
method of the Isolated Storage object. The OpenFile
method returns a stream object of type, IsolatedStorageFileStream
. The stream object has a Name property. The Name property is the file path in the Windows phone file system.
That is a very indirect way of getting the file path of a SQLite database. But once we get the path, create a connection object using the path, just like we usually do.
Fortunately, there is a better way to get the file path. Write an extension method. The extension method accepts a file name and return the file path. In that way, we do not have to repeat the code elsewhere.