无法连接到服务器“ xxx”,但在Visual Studio Start中有效

问题描述

我正在尝试在远程开发服务器上备份数据库,并将其发送到我和开发PC之间共享的网络驱动器文件夹中。

我已经附加了我正在调用功能和我也收到的错误

但是,当我使用顶部的Visual Studio的“开始”按钮运行此程序时,它运行得很好(在管理模式下运行)

每个人都在说权限,但是我不明白在开发服务器上如何考虑我PC上的权限

public static string BackupDatabase(string DatabaseName)
        {
            String sqlServerLogin = Properties.Resources.Username;
            String password = Properties.Resources.Password;
            String remoteSvrName = Properties.Resources.DataSource;

            // Authentication
            ServerConnection srvConn2 = new ServerConnection(remoteSvrName);
            srvConn2.LoginSecure = false;
            srvConn2.Login = sqlServerLogin;
            srvConn2.Password = password;
            
            // Initialisation of the server object
            Server srv = new Server(srvConn2);

            Backup bk = new Backup();
            bk.Action = BackupActionType.Database;
            bk.BackupSetDescription = "Staging Transfer";
            bk.BackupSetName = DatabaseName + " Backup";
            bk.Database = DatabaseName;

            // Declare a BackupDeviceItem by supplying the backup device file name in the constructor,and the type of device is a file.   
            BackupDeviceItem bdi = default(BackupDeviceItem);
            string backupPath = Properties.Resources.DatabaseBackupPath + DatabaseName + DateTime.Now.ToString("ddMMyyyy") + ".bak";
            if (File.Exists(backupPath))
            {
                File.Delete(backupPath);
            }
            bdi = new BackupDeviceItem(backupPath,DeviceType.File);
            // Add the device to the Backup object.   
            bk.Devices.Add(bdi);

            bk.Checksum = true;
            bk.ContinueAfterError = true;
            bk.Initialize = true;
            // Set the Incremental property to False to specify that this is a full database backup.   
            bk.Incremental = false;
           
            // Specify that the log must be truncated after the backup is complete.   
            bk.LogTruncation = BackupTruncateLogType.Truncate;

            // Run sqlBackup to perform the full database backup on the instance of sql Server.   
            bk.sqlBackup(srv);
            bk.Wait();

            return backupPath;
        }

有人可以为此建议我吗?这让我发疯了!

private void backupDatabaseWorker_DoWork(object sender,DoWorkEventArgs e)
        {
            backupDatabaseWorker.WorkerReportsProgress = true;
            Utilities util = new Utilities();
            string database = util.GetDatabase(filePathSelected.Text);
            string backupReturn = sql.BackupDatabase(database);
            backupPath = backupReturn;
            MessageBox.Show(database);
            MessageBox.Show(backupReturn);
        }

GetDatabase函数工作正常,该函数使用Web配置获取站点使用的数据库名称

任何帮助将不胜感激

错误

Failed to connect to server dev.   at Microsoft.sqlServer.Management.Common.ConnectionManager.Connect()
   at Microsoft.sqlServer.Management.Common.ConnectionManager.PoolConnect()
   at Microsoft.sqlServer.Management.Common.ConnectionManager.GetServerinformation()
   at Microsoft.sqlServer.Management.Smo.ExecutionManager.GetServerVersion()
   at Microsoft.sqlServer.Management.Smo.sqlSmoObject.get_ServerVersion()
   at Microsoft.sqlServer.Management.Smo.Backup.Script(Server targetServer)
   at Microsoft.sqlServer.Management.Smo.Backup.sqlBackup(Server srv)
   at Microsoft.sqlServer.Management.Smo.Backup.sqlBackup(Server srv)
   at PCS_Lithium.sql.BackupDatabase(String DatabaseName)
   at PCS_Lithium.PushToStagingForm.backupDatabaseWorker_DoWork(Object sender,DoWorkEventArgs e)
   at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
Microsoft.sqlServer.SmoExtended

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)