Ejemplo n.º 1
0
        private bool CopyFileIntoProjectAndConvertConnectionString(string filePath)
        {
            try
            {
                // File is not in the project and user wants to convert it.
                var serviceProvider         = Wizard.ServiceProvider;
                var project                 = Wizard.Project;
                var vsTrackProjectDocuments = serviceProvider.GetService(typeof(SVsTrackProjectDocuments)) as IVsTrackProjectDocuments3;

                if (null == vsTrackProjectDocuments)
                {
                    Debug.Fail("Could not get IVsTrackProjectDocuments3 from service provider.");
                }
                else
                {
                    // releases any read locks on this file so we can read it
                    vsTrackProjectDocuments.HandsOffFiles((uint)__HANDSOFFMODE.HANDSOFFMODE_ReadAccess, 1, new[] { filePath });
                }

                ProjectItem dbProjectItem = null;
                var         targetProjectItemCollection = LocalDataUtil.GetDefaultCollectionForLocalDataFile(Wizard.ServiceProvider, project);
                dbProjectItem = VsUtils.BringDatabaseFileIntoProject(serviceProvider, project, targetProjectItemCollection, filePath);

                if (dbProjectItem == null)
                {
                    var errmsg = string.Format(CultureInfo.CurrentCulture, Resources.LocalDataErrorAddingFile, filePath, project.UniqueName);
                    throw new FileCopyException(errmsg);
                }

                var newFilePath = (string)dbProjectItem.Properties.Item("FullPath").Value;

                // now re-target the connection string at the copied file
                return(RetargetConnectionString(newFilePath));
            }
            catch (COMException ex)
            {
                if (ex.ErrorCode == VSConstants.OLE_E_PROMPTSAVECANCELLED)
                {
                    // User canceled a checkout prompt.
                    return(false);
                }
                else
                {
                    throw;
                }
            }
        }