Reputation: 27
I've already created the database and I'm trying to make the sqliteconnection and link it to the filepath of the database, which i've placed on the simulated sdcard.
This is my code for the database connection:
public static class DatabaseManager
{
public static SQLiteConnection dbConnection = new SQLiteConnection("MusicAppM.db");
public static async void CreateMobileDB()
{
StorageFile db = null;
StorageFolder folder = KnownFolders.RemovableDevices;
await RetrieveDbInFolders(db, folder);
string ConnString = Path.Combine(db.Path, "MusicAppM.db");
SQLiteConnection dbConnection = new SQLiteConnection(ConnString);
}
private static async Task RetrieveDbInFolders(StorageFile db, StorageFolder parent)
{
foreach (var item in await parent.GetFilesAsync())
{
if (item.FileType == ".db")
db = item;
}
foreach (var item in await parent.GetFoldersAsync())
{
await RetrieveDbInFolders(db, item);
}
}
}
However, my function "retrievedbinfolders" never returns the database file even though I've placed it in the folder which is the simulated sdcard. Why is this?
On a second note I'm not sure if the path.combine will link the database connection to the filepath, so I'm contemplating wether or not I'm better of just setting up a server instead.
Upvotes: 0
Views: 122
Reputation: 248
1.why the function "retrievedbinfolders" never returns the database file ?
When an object instance is passed as an argument to a method, the value of the parameter is a reference to the object. The contents of the object can be changed in the called method, but the object reference is never changed
code :
public static class DatabaseManager
{
public static SQLiteConnection dbConnection = new SQLiteConnection(new SQLitePlatformWinRT(), "MusicAppM.db");
// private static StorageFile db = null;
public static async void CreateMobileDB()
{
StorageFile db = null;
// StorageFolder folder = KnownFolders.RemovableDevices;
StorageFolder folder = ApplicationData.Current.LocalFolder;
await RetrieveDbInFolders(db, folder, (parm) =>
{ db = parm; });
if (db != null)
{
SQLiteConnection dbConnection = new SQLiteConnection(new SQLitePlatformWinRT(), db.Path);
}
else
{
string ConnString = Path.Combine(folder.Path, "MusicAppM.db");
SQLiteConnection dbConnection = new SQLiteConnection(new SQLitePlatformWinRT(), ConnString);
}
}
private static async Task RetrieveDbInFolders(StorageFile db, StorageFolder parent, Action<StorageFile> callback = null)
{
foreach (var item in await parent.GetFilesAsync())
{
if (item.FileType == ".db")
db = item;
callback(db);
}
foreach (var item in await parent.GetFoldersAsync())
{
await RetrieveDbInFolders(db, item);
}
}
}
This may be you want
Upvotes: 1