/// <summary> /// Инициализировать новое подключение к локальной базе данных /// </summary> /// <param name="fileName">Название файла в локальном хранилище</param> /// <param name="tables">Таблицы, содержащиеся в базе данных</param> /// <param name="access">Режим доступа</param> /// <param name="appxUri">Ссылка на исходную версию базы данных</param> public DatabaseBase(string fileName, string[] tables, SQLiteOpen access, Uri appxUri, bool doSetDefault = false) { this.FileName = fileName; this.Tables = tables; this.AppxUri = appxUri; if (doSetDefault) { AsyncHelper.RunSync(this.SetDefaultAsync); } this.con = new SQLiteConnection(fileName, access); this.ActivateForeignKeys(); }
private SQLiteConnection(string fileName, SQLiteOpen openFlag, bool setTemporaryDirectory) { this.platformMarshal = Platform.Instance.PlatformMarshal; this.platformStorage = Platform.Instance.PlatformStorage; this.sqlite3Provider = Platform.Instance.SQLite3Provider; if (setTemporaryDirectory) { this.SetTemporaryDirectory(); } var localFilePath = string.Empty; if (fileName.Trim().ToLowerInvariant() == ":memory:") { localFilePath = ":memory:"; } else if (fileName.Trim() != string.Empty) { localFilePath = this.platformStorage.GetLocalFilePath(fileName); } var fileNamePtr = this.platformMarshal.MarshalStringManagedToNativeUTF8(localFilePath); int flags; switch (openFlag) { case SQLiteOpen.READONLY: // URI|DONTCREATE|READONLY flags = 0x41; break; case SQLiteOpen.READWRITE: // URI|CREATE|READWRITE flags = 0x46; break; default: // URI|CREATE|READWRITE flags = 0x46; break; } try { var openResult = (SQLiteResult)this.sqlite3Provider.Sqlite3Open(fileNamePtr, out this.db, flags); if (openResult != SQLiteResult.OK) { if (this.db != IntPtr.Zero) { var errmsgPtr = this.sqlite3Provider.Sqlite3Errmsg(this.db); var errmsg = this.platformMarshal.MarshalStringNativeUTF8ToManaged(errmsgPtr); this.sqlite3Provider.Sqlite3CloseV2(this.db); throw new SQLiteException("Unable to open the database file: " + fileName + " Details: " + errmsg); } else { throw new SQLiteException("Unable to open the database file: " + fileName + " Details: " + openResult.ToString()); } } } catch (SQLiteException) { throw; } catch (Exception ex) { throw new SQLiteException("Unable to open the database file: " + fileName, ex); } finally { if (fileNamePtr != IntPtr.Zero) { this.platformMarshal.CleanUpStringNativeUTF8(fileNamePtr); } } }
public SQLiteConnection(string fileName, SQLiteOpen openFlag) : this(fileName, openFlag, true) { }
/// <summary> /// Инициализировать новое подключение к локальной базе данных /// </summary> /// <param name="fileName">Название файла в локальном хранилище</param> /// <param name="tables">Таблицы, содержащиеся в базе данных</param> /// <param name="access">Режим доступа</param> public DatabaseBase(string fileName, string[] tables, SQLiteOpen access) : this(fileName, tables, access, new Uri("ms-appx:///Data/" + fileName)) { }