无法使用C#错误创建SQL Server(网络模式)数据库的备份:备份数据库异常终止 [英] Unable to create backup of SQL server (network mode) database using C# error: backup database terminating abnormally

查看:86
本文介绍了无法使用C#错误创建SQL Server(网络模式)数据库的备份:备份数据库异常终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
                   {
                       string q = "BACKUP DATABASE [CRMProduction] TO DISK= '" + txtLocation1.Text + "\\" + "Database" + "-" + System.DateTime.Now.Ticks.ToString() + ".bak'";
                                //+ " WITH FORMAT, MEDIANAME = 'Z_SQLServerBackups', NAME = 'Full Backup of CRMProduction';";
                       using (con = new SqlConnection(CS))
                       {
                           cmd = new SqlCommand(q, con);
                           if (con.State != ConnectionState.Open)
                           {
                               con.Open();
                           }
                           cmd.ExecuteNonQuery();
                           con.Close();

                           MessageBox.Show("Backup Completed...");
                           btnBackup.Enabled = true;
                           progressBar1.Visible = false;
                           lblProgressPercent.Visible = false;
                           btnCancel.Visible = false;
                           btnBackup.Enabled = true;
                           btnBrowse1.Enabled = true;
                           rdoBack.Checked = false;
                           rdoRest.Checked = false;
                           progressBar1.Value = 0;
                       }
                   }
                   catch (Exception ex)
                   {
                       MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   }





我的尝试:



i想创建备份&使用C#进行编码恢复。我写了一些代码。但我得到的错误是:无法打开备份设备'C:\ Users \Xyz \Desktop \ bck \Database-636087712631488222.bak'。操作系统错误5(访问被拒绝。)。 BACKUP DATABASE正在异常终止。



我正在使用带有Sql Server身份验证模式的SQLSERVER 2008R2。 &安培;我的SQLSERVER在网络中。



&我知道如何手动创建备份或在SQL中使用脚本。但我的问题在于C#代码。如果有人知道,请帮忙。



What I have tried:

i want to create backup & restore by coding using C#. i wrote some codes. but i got error which says: Cannot open backup device 'C:\Users\Xyz\Desktop\bck\Database-636087712631488222.bak'. Operating system error 5(Access is denied.). BACKUP DATABASE is terminating abnormally.

i'm using SQLSERVER 2008R2 with Sql Server Authentication mode. & my SQLSERVER is in Network.

& i know that how to create backup manually or with script in SQL. but my problem is in C# code. please help if anybody knows.

推荐答案

看起来你正试图将数据库的备份放在本地机器上的文件夹中。如果你懒得阅读它,错误是非常明显的。 SQL Server根本没有对您的计算机的权限,因此它无法在那里编写备份。
It looks like you're trying to put the backup of the database in a folder on your local machine. The error is pretty obvious if you bother to read it. The SQL Server does not have permissions to your machine at all so it can't write the backup there.


正如Dave所说,这是一个文件夹问题。

如果你看一下这个:用C#备份SQL数据库 [ ^ ]它提供了我使用的代码,并解释了您需要注意的缺陷 - 包括(但不限于)您遇到的文件夹访问问题。



并且永远不会连接字符串以形成SQL命令 - 甚至不是BACKUP - 您可以将代码保持为SQL注入。始终使用参数化查询!
As Dave says, it's a folder problem.
If you have a look at this: Backing up an SQL Database in C#[^] it provides the code I use, and explains the pitfalls you have to look out for - whihc includes (but isn't limited to) the folder access problem you are having.

And never concatenate strings to form an SQL command - not even a BACKUP - you leave your code wide open to SQL Injection. Always use a parameterised query!


这篇关于无法使用C#错误创建SQL Server(网络模式)数据库的备份:备份数据库异常终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆