public Dictionary <Xyz, byte[]> GetChunksFromFile(IEnumerable <Xyz> chunkpositions, string filename) { Dictionary <Xyz, byte[]> chunks = new Dictionary <Xyz, byte[]>(); if (!File.Exists(filename)) { Console.WriteLine(string.Format("File {0} does not exist.", filename)); return(null); } StringBuilder b = new StringBuilder(); DbConnectionStringBuilder.AppendKeyValuePair(b, "Data Source", filename); DbConnectionStringBuilder.AppendKeyValuePair(b, "Version", "3"); DbConnectionStringBuilder.AppendKeyValuePair(b, "New", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Compress", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Journal Mode", "Off"); SQLiteConnection conn = new SQLiteConnection(b.ToString()); conn.Open(); using (SQLiteTransaction transaction = conn.BeginTransaction()) { foreach (var xyz in chunkpositions) { ulong pos = MapUtil.ToMapPos(xyz.X, xyz.Y, xyz.Z); chunks.Add(xyz, GetChunkFromFile(pos, conn)); } transaction.Commit(); conn.Close(); conn.Dispose(); } return(chunks); }
public void Open(string filename) { databasefile = filename; bool newdatabase = false; if (!File.Exists(databasefile)) { newdatabase = true; } StringBuilder b = new StringBuilder(); DbConnectionStringBuilder.AppendKeyValuePair(b, "Data Source", databasefile); DbConnectionStringBuilder.AppendKeyValuePair(b, "Version", "3"); DbConnectionStringBuilder.AppendKeyValuePair(b, "New", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Compress", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Journal Mode", "Off"); sqliteConn = new SQLiteConnection(b.ToString()); sqliteConn.Open(); if (newdatabase) { CreateTables(sqliteConn); } if (!integrityCheck(sqliteConn)) { Console.WriteLine("Database is possibly corrupted."); //repair(sqliteConn); } //vacuum(sqliteBckConn); }
public void Backup(string backupFilename) { if (databasefile == backupFilename) { Console.WriteLine(string.Format("Cannot overwrite current running database. Chose another destination.")); return; } if (File.Exists(backupFilename)) { Console.WriteLine(string.Format("File {0} exists. Overwriting file.", backupFilename)); } StringBuilder b = new StringBuilder(); DbConnectionStringBuilder.AppendKeyValuePair(b, "Data Source", backupFilename); DbConnectionStringBuilder.AppendKeyValuePair(b, "Version", "3"); DbConnectionStringBuilder.AppendKeyValuePair(b, "New", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Compress", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Journal Mode", "Off"); SQLiteConnection sqliteBckConn = new SQLiteConnection(b.ToString()); sqliteBckConn.Open(); sqliteConn.BackupDatabase(sqliteBckConn, sqliteBckConn.Database, sqliteConn.Database, -1, null, 10); // shrink database vacuum(sqliteBckConn); sqliteBckConn.Close(); sqliteBckConn.Dispose(); }
// <Snippet1> public string AddPooling(string connectionString) { StringBuilder builder = new StringBuilder(connectionString); DbConnectionStringBuilder.AppendKeyValuePair(builder, "Pooling", "true"); return(builder.ToString()); }
/// <summary> /// Builds a connection string from the passed in parameters. /// </summary> /// <returns></returns> private string GetConnectionString() { if (!string.IsNullOrEmpty(this.Parameters.ConnectionString)) { return(this.Parameters.ConnectionString); } if (!Uri.TryCreate(this.Parameters.Url, UriKind.RelativeOrAbsolute, out var uri)) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot connect to organization service at {0}", this.Parameters.Url)); } var stringBuilder = new StringBuilder(); DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "Server", uri.ToString()); DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "UserName", this.Parameters.UserName); DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "Password", this.Parameters.Password); if (!string.IsNullOrEmpty(this.Parameters.Domain)) { DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "Domain", this.Parameters.Domain); DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "AuthType", "AD"); } else if (this.Parameters.UseOAuth) { DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "AuthType", "OAuth"); DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "ClientId", "2ad88395-b77d-4561-9441-d0e40824f9bc"); DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "RedirectUri", "app://5d3e90d6-aa8e-48a8-8f2c-58b45cc67315"); DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "LoginPrompt", "Never"); } else { DbConnectionStringBuilder.AppendKeyValuePair(stringBuilder, "AuthType", "Office365"); } return(stringBuilder.ToString()); }
public void SetChunksToFile(IEnumerable <DbChunk> chunks, string filename) { bool newDatabase = true; if (databasefile == filename) { Console.WriteLine(string.Format("Cannot overwrite current running database. Chose another destination.")); return; } if (File.Exists(filename)) { Console.WriteLine(string.Format("File {0} exists. Overwriting file.", filename)); newDatabase = false; } StringBuilder b = new StringBuilder(); DbConnectionStringBuilder.AppendKeyValuePair(b, "Data Source", filename); DbConnectionStringBuilder.AppendKeyValuePair(b, "Version", "3"); DbConnectionStringBuilder.AppendKeyValuePair(b, "New", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Compress", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Journal Mode", "Off"); SQLiteConnection sqliteConn = new SQLiteConnection(b.ToString()); sqliteConn.Open(); if (newDatabase) { CreateTables(sqliteConn); } using (SQLiteTransaction transaction = sqliteConn.BeginTransaction()) { foreach (DbChunk c in chunks) { ulong pos = MapUtil.ToMapPos(c.Position.X, c.Position.Y, c.Position.Z); DbCommand cmd = sqliteConn.CreateCommand(); cmd.CommandText = "INSERT OR REPLACE INTO chunks (position, data) VALUES (?,?)"; cmd.Parameters.Add(CreateParameter("position", DbType.UInt64, pos, cmd)); cmd.Parameters.Add(CreateParameter("data", DbType.Object, c.Chunk, cmd)); cmd.ExecuteNonQuery(); } transaction.Commit(); } sqliteConn.Close(); sqliteConn.Dispose(); }
public void Open(string filename) { string databasefile = filename; bool newdatabase = false; if (!File.Exists(databasefile)) { newdatabase = true; } StringBuilder b = new StringBuilder(); DbConnectionStringBuilder.AppendKeyValuePair(b, "Data Source", databasefile); DbConnectionStringBuilder.AppendKeyValuePair(b, "Version", "3"); DbConnectionStringBuilder.AppendKeyValuePair(b, "New", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Compress", "True"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Journal Mode", "Off"); sqliteConn = new SQLiteConnection(b.ToString()); sqliteConn.Open(); if (newdatabase) { CreateTables(); } }
private Dictionary <string, OleDbPropertyInfo> GetProviderInfo(string provider) { Dictionary <string, OleDbPropertyInfo> dictionary = this._propertyInfo; if (dictionary == null) { dictionary = new Dictionary <string, OleDbPropertyInfo>(StringComparer.OrdinalIgnoreCase); if (!ADP.IsEmpty(provider)) { Dictionary <string, OleDbPropertyInfo> propertyInfo = null; try { StringBuilder builder = new StringBuilder(); DbConnectionStringBuilder.AppendKeyValuePair(builder, "Provider", provider); OleDbConnectionString constr = new OleDbConnectionString(builder.ToString(), true); constr.CreatePermissionSet().Demand(); using (OleDbConnectionInternal internal2 = new OleDbConnectionInternal(constr, null)) { Guid[] propertySets = new Guid[] { OleDbPropertySetGuid.DBInitAll }; propertyInfo = internal2.GetPropertyInfo(propertySets); foreach (KeyValuePair <string, OleDbPropertyInfo> pair3 in propertyInfo) { Keywords keywords; OleDbPropertyInfo info2 = pair3.Value; if (!_keywords.TryGetValue(info2._description, out keywords) && ((OleDbPropertySetGuid.DBInit != info2._propertySet) || (((200 != info2._propertyID) && (60 != info2._propertyID)) && (0x40 != info2._propertyID)))) { dictionary[info2._description] = info2; } } List <Guid> list = new List <Guid>(); foreach (KeyValuePair <string, OleDbPropertyInfo> pair2 in propertyInfo) { OleDbPropertyInfo info3 = pair2.Value; if (!list.Contains(info3._propertySet)) { list.Add(info3._propertySet); } } Guid[] array = new Guid[list.Count]; list.CopyTo(array, 0); using (PropertyIDSet set2 = new PropertyIDSet(array)) { using (IDBPropertiesWrapper wrapper = internal2.IDBProperties()) { OleDbHResult result; using (DBPropSet set = new DBPropSet(wrapper.Value, set2, out result)) { if (OleDbHResult.S_OK <= result) { int propertySetCount = set.PropertySetCount; for (int i = 0; i < propertySetCount; i++) { Guid guid; foreach (tagDBPROP gdbprop in set.GetPropertySet(i, out guid)) { foreach (KeyValuePair <string, OleDbPropertyInfo> pair in propertyInfo) { OleDbPropertyInfo info = pair.Value; if ((info._propertyID == gdbprop.dwPropertyID) && (info._propertySet == guid)) { info._defaultValue = gdbprop.vValue; if (info._defaultValue == null) { if (typeof(string) == info._type) { info._defaultValue = ""; } else if (typeof(int) == info._type) { info._defaultValue = 0; } else if (typeof(bool) == info._type) { info._defaultValue = false; } } } } } } } } } } } } catch (InvalidOperationException exception3) { ADP.TraceExceptionWithoutRethrow(exception3); } catch (OleDbException exception2) { ADP.TraceExceptionWithoutRethrow(exception2); } catch (SecurityException exception) { ADP.TraceExceptionWithoutRethrow(exception); } } this._propertyInfo = dictionary; } return(dictionary); }
public static void AppendKeyValuePair(StringBuilder builder, string keyword, string value, bool useOdbcRules) { DbConnectionStringBuilder.AppendKeyValuePair(builder, keyword, value, useOdbcRules); }
public static void AppendKeyValuePair(StringBuilder builder, string keyword, string value) { DbConnectionStringBuilder.AppendKeyValuePair(builder, keyword, value); }
public bool OpenOrCreate(string filename, ref string errorMessage, bool requireWriteAccess, bool corruptionProtection, bool doIntegrityCheck) { databaseFileName = filename; if (!File.Exists(databaseFileName)) { if (!HaveWriteAccessFolder(Path.GetDirectoryName(filename))) { throw new IOException(string.Format("Cannot create {2} file {0}, the folder {1} does not seem to be writable!", filename, Path.GetDirectoryName(filename), DBTypeCode)); } } else { if (!HaveWriteAccessFile(new FileInfo(filename))) { if (requireWriteAccess) { throw new IOException(string.Format("Cannot open {1} file {0}, it seems to be not writable!", filename, DBTypeCode)); } IsReadOnly = true; } } try { StringBuilder b = new StringBuilder(); DbConnectionStringBuilder.AppendKeyValuePair(b, "Data Source", databaseFileName); DbConnectionStringBuilder.AppendKeyValuePair(b, "Version", "3"); DbConnectionStringBuilder.AppendKeyValuePair(b, "New", "True"); // Create new file if it doesnt exist DbConnectionStringBuilder.AppendKeyValuePair(b, "Compress", "True"); if (corruptionProtection) { DbConnectionStringBuilder.AppendKeyValuePair(b, "Journal Mode", "WAL"); DbConnectionStringBuilder.AppendKeyValuePair(b, "Synchronous", "Normal"); } else { DbConnectionStringBuilder.AppendKeyValuePair(b, "Journal Mode", "Off"); } sqliteConn = new SQLiteConnection(b.ToString()); sqliteConn.Open(); if (requireWriteAccess) { CreateTablesIfNotExists(sqliteConn); } if (doIntegrityCheck) { if (!DoIntegrityCheck(sqliteConn)) { logger.Error(errorMessage = "Database is possibly corrupted."); } } } catch (Exception e) { logger.Error(errorMessage = "Failed opening savegame." + e); return(false); } OnOpened(); return(true); }