protected void CloseConnection() { if (this.connection != null) { this.connection.Close(); this.connection.Dispose(); this.connection = null; } if (this.objectContext != null) { this.objectContext.Dispose(); this.objectContext = null; } }
private bool OpenConnection(bool silent = false) { if (this.connection != null && this.connection.State == ConnectionState.Open) { return(true); } try { DbConnection dbConnection; if (this.DatabaseExist(out dbConnection)) { this.connection = this.OpenEntityConnection(dbConnection); this.objectContext = new ClousotCacheEntities(this.connection); return(true); } dbConnection = this.CreateDatabase(); this.connection = this.OpenEntityConnection(dbConnection); using (var trans = this.connection.StoreConnection.BeginTransaction()) { using (var cmd = this.connection.StoreConnection.CreateCommand()) { cmd.Transaction = trans; foreach (var query in this.TablesCreationQueries.Split(';')) { if (String.IsNullOrWhiteSpace(query)) { continue; } cmd.CommandText = query; cmd.ExecuteNonQuery(); } } trans.Commit(); } this.objectContext = new ClousotCacheEntities(this.connection); foreach (var m in this.metadataIfCreation) { this.objectContext.AddToCachingMetadatas(new CachingMetadata { Key = m.Key, Value = m.Value }); } this.TrySaveChanges(); return(true); } catch (Exception e) { if (!(e is SqlCeException || e is SqlException || e is IOException || e is DataException)) { throw; } if (!silent) { Console.WriteLine("Error: unable to open the cache file: " + e.Message); } this.CloseConnection(); } return(false); }
private VersionResult[] ReadChartPoints(string FileName) { Contract.Requires(!String.IsNullOrWhiteSpace(FileName)); Contract.Ensures(Contract.Result <VersionResult[]>() != null); VersionResult[] res; var ConnectionString = "data source='" + FileName + "'";//; mode='Read Only';"; doesn't work // read the sql DB var entityConnectionString = new EntityConnectionStringBuilder { Metadata = "res://*/ClousotCacheModel.csdl|res://*/ClousotCacheModel.ssdl|res://*/ClousotCacheModel.msl", Provider = "System.Data.SqlServerCe.3.5", ProviderConnectionString = ConnectionString }.ToString(); using (var connection = new EntityConnection(entityConnectionString)) { connection.Open(); using (var objectContext = new ClousotCacheEntities(connection)) { objectContext.ContextOptions.LazyLoadingEnabled = true; // avoid having to manually load every method var resList = new List <VersionResult>(); #if false var versionMethods = objectContext.VersionBindings.GroupBy(b => b.Version); var versionResults = objectContext.VersionResults.ToDictionary(r => r.Version); foreach (var versionMethod in versionMethods) { VersionResult versionResult; // Sanity check if (versionResults.TryGetValue(versionMethod.Key, out versionResult) && versionResult.Methods == versionMethod.LongCount()) { versionResult.Complete(); // previously computed values can be reused } else { if (versionResult != null) // the entry needs an update { objectContext.DeleteObject(versionResult); } versionResult = new VersionResult(versionMethod.Key, versionMethod.Select(b => b.Method)); // recompute objectContext.VersionResults.AddObject(versionResult); } resList.Add(versionResult); } objectContext.SaveChanges(); foreach (var versionResult in resList) { objectContext.Detach(versionResult); } #endif res = resList.ToArray(); #if false var points = objectContext.VersionBindings.Include("Method").GroupBy(b => b.Version, b => b.Method, ChartPoint.Create); res = points.OrderBy(m => m.Version).ToArray(); #endif } connection.Close(); } return(res); }