internal static void RemoveDataConnection(string connectionString)
        {
            var storeType = GetPreferredDatabaseType();
            var helper    = RepositoryHelper.CreateEngineHelper(storeType);

            helper.DeleteDataConnnection(CreateStore(storeType), connectionString);
        }
        internal static void RenameDataConnection(string connectionString, string description)
        {
            var storeType = GetPreferredDatabaseType();
            var helper    = RepositoryHelper.CreateEngineHelper(storeType);

            helper.UpdateDataConnection(CreateStore(storeType), connectionString, description);
        }
        private static string CreateStore(DatabaseType storeDbType)
        {
            string fileName   = GetStoreName(storeDbType);
            string connString = string.Format("Data Source={0};", fileName);

            if (!File.Exists(fileName))
            {
                if (storeDbType == DatabaseType.SQLite)
                {
                    var helper = RepositoryHelper.CreateEngineHelper(storeDbType);
                    helper.CreateDatabase(connString);
                }
                else
                {
                    var sdf = Resources.SqlCe35AddinStore;
                    if (storeDbType == DatabaseType.SQLCE40)
                    {
                        sdf = Resources.SqlCe40AddinStore;
                    }
                    using (Stream stream = new MemoryStream(sdf))
                    {
                        // Create a FileStream object to write a stream to a file
                        using (FileStream fileStream = File.Create(fileName, (int)stream.Length))
                        {
                            // Fill the bytes[] array with the stream data
                            byte[] bytesInStream = new byte[stream.Length];
                            stream.Read(bytesInStream, 0, bytesInStream.Length);
                            // Use FileStream object to write to the specified file
                            fileStream.Write(bytesInStream, 0, bytesInStream.Length);
                        }
                    }
                }
            }

            var dbInfo = new DatabaseInfo {
                DatabaseType = storeDbType, ConnectionString = connString
            };

            using (IRepository repository = Helpers.RepositoryHelper.CreateRepository(dbInfo))
            {
                var tables = repository.GetAllTableNames();
                if (!tables.Contains("Databases"))
                {
                    var script =
                        "CREATE TABLE Databases (Id INT IDENTITY, Source nvarchar(2048) NOT NULL, FileName nvarchar(512) NOT NULL, CeVersion int NOT NULL)" +
                        separator;
                    if (storeDbType == DatabaseType.SQLite)
                    {
                        script =
                            "CREATE TABLE Databases (Id INTEGER PRIMARY KEY, Source nvarchar(2048) NOT NULL, FileName nvarchar(512) NOT NULL, CeVersion int NOT NULL)" +
                            separator;
                    }
                    repository.ExecuteSql(script);
                }
            }
            return(connString);
        }
예제 #4
0
        internal void ScanConnections(SqlCeToolboxPackage package)
        {
            var          dte       = package.GetServiceHelper(typeof(DTE)) as DTE2;
            var          helper    = RepositoryHelper.CreateEngineHelper(DatabaseType.SQLCE40);
            EnvDteHelper dteHelper = new EnvDteHelper();
            var          list      = dteHelper.GetSqlCeFilesInActiveSolution(dte);

            foreach (var path in list)
            {
                if (File.Exists(path))
                {
                    bool         versionFound = false;
                    SQLCEVersion version      = SQLCEVersion.SQLCE20;
                    try
                    {
                        version      = helper.DetermineVersion(path);
                        versionFound = true;
                    }
                    catch
                    {
                        // ignored
                    }
                    string connectionString = string.Format("Data Source={0}", path);
                    if (versionFound)
                    {
                        if (version == SQLCEVersion.SQLCE35)
                        {
                            SaveDataConnection(connectionString, DatabaseType.SQLCE35, package);
                        }
                        else if (version == SQLCEVersion.SQLCE40)
                        {
                            SaveDataConnection(connectionString, DatabaseType.SQLCE40, package);
                        }
                    }
                    else
                    {
                        var dbInfo = new DatabaseInfo()
                        {
                            DatabaseType     = DatabaseType.SQLite,
                            ConnectionString = connectionString
                        };
                        try
                        {
                            using (var repo = Helpers.RepositoryHelper.CreateRepository(dbInfo))
                            {
                                repo.GetAllTableNames();
                            }
                            SaveDataConnection(connectionString, DatabaseType.SQLite, package);
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                }
            }
        }
        internal static void SaveDataConnection(string connectionString, DatabaseType dbType,
                                                SqlCeToolboxPackage package)
        {
            var    storeDbType = GetPreferredDatabaseType();
            var    helper      = RepositoryHelper.CreateEngineHelper(storeDbType);
            string path        = RepositoryHelper.CreateEngineHelper(dbType).PathFromConnectionString(connectionString);

            if (package.VsSupportsSimpleDdex4Provider() && dbType == DatabaseType.SQLCE40)
            {
                SaveDataConnection(package, DataProtection.EncryptString(connectionString), dbType,
                                   new Guid(Resources.SqlCompact40PrivateProvider));
            }
            else
            {
                helper.SaveDataConnection(CreateStore(storeDbType), connectionString, path, dbType.GetHashCode());
            }
        }
        internal static string SendError(Exception ex, DatabaseType dbType, bool report = true)
        {
            if (ex != null)
            {
                var dontTrack = ex.GetType().Name == "SqlCeException" ||
                                ex.GetType().Name == "SqlCeInvalidDatabaseFormatException" ||
                                ex is SqlException ||
                                ex is DBConcurrencyException ||
                                ex is SQLiteException;

                if (!dontTrack && report)
                {
                    Telemetry.TrackException(ex);
                }
                EnvDteHelper.ShowError(RepositoryHelper.CreateEngineHelper(dbType).FormatError(ex));
            }
            return(string.Empty);
        }
        public static string GetFilePath(string connectionString, DatabaseType dbType)
        {
            var helper = RepositoryHelper.CreateEngineHelper(dbType);

            return(helper.PathFromConnectionString(connectionString));
        }