微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决无法连接到服务器“ 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 举报,一经查实,本站将立刻删除。