Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SMO从数据库进行备份失败_C#_Winforms_Sql Server 2008 R2_Backup_Smo - Fatal编程技术网

C# 使用SMO从数据库进行备份失败

C# 使用SMO从数据库进行备份失败,c#,winforms,sql-server-2008-r2,backup,smo,C#,Winforms,Sql Server 2008 R2,Backup,Smo,使用SMO从SQL Server 2008 R2数据库进行的备份不起作用: 备份数据库异常终止。 Source=.Net SqlClient数据提供程序 错误代码=-2146232060 等级=16 行号=1 数字=3201 程序=” 服务器=(本地) 状态=1 StackTrace: InnerException:Microsoft.SqlServer.Management.Common.ExecutionFailureException 消息=执行Transact-SQL语句或批处理时发生异

使用SMO从SQL Server 2008 R2数据库进行的备份不起作用:

备份数据库
异常终止。
Source=.Net SqlClient数据提供程序
错误代码=-2146232060
等级=16
行号=1
数字=3201
程序=”
服务器=(本地)
状态=1

StackTrace:
InnerException:Microsoft.SqlServer.Management.Common.ExecutionFailureException
消息=执行Transact-SQL语句或批处理时发生异常。
Source=Microsoft.SqlServer.ConnectionInfo

StackTrace:
位于Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteOnQuery(字符串sqlCommand,ExecutionTypes executionType)
位于Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteOnQuery(StringCollection sqlCommands,ExecutionTypes executionType)
在Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteOnQuery(StringCollection查询)中
在Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(服务器,StringCollection查询)
位于Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(服务器srv)

InnerException:System.Data.SqlClient.SqlException
在Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction操作,对象ExecutObject,数据集fillDataSet,布尔catchException)
位于Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteOnQuery(字符串sqlCommand,ExecutionTypes executionType)

我的代码:

class Backup_Restore 
{
    public string BackUpConString = @"Data Source=(local);Initial Catalog=taban;Integrated Security=True";
public string ReStoreConString = "Data Source=(local);Initial Catalog=master;Integrated Security=True";

    public void BackUpMyDB()
    {
        using (SqlConnection con = new SqlConnection(BackUpConString))
        {
            ServerConnection srvConn = new ServerConnection(con);

            Server srvr = new Server(srvConn);

            if (srvr != null)
            {
                try
                {
                    Backup bkpDatabase = new Backup();
                    bkpDatabase.Action = BackupActionType.Database;
                    bkpDatabase.Database = "taban";
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "BackUp File|*.taban";
                    sfd.FileName = "BackUp_" + (DateTime.Now.ToShortDateString().Replace('/', '.'));
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        BackupDeviceItem bkpDevice = new BackupDeviceItem(sfd.FileName, DeviceType.File);
                        bkpDatabase.Devices.Add(bkpDevice);
                        bkpDatabase.SqlBackup(srvr);

                        MessageBox.Show("Bakup of Database successfully created", "Server", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception e) { 
                    MessageBox.Show(e.ToString()); }
            }
        }
    }
       public void ReStorMyDB()
    {
        if (MessageBox.Show("All Data Stored in the Database may change!!! \n If you agree, select \"Yes\".", "DataBase ReStore", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {

            SqlConnection.ClearAllPools();
            using (SqlConnection con = new SqlConnection(BackUpConString))
            {
                ServerConnection srvConn = new ServerConnection(con);

                Server srvr = new Server(srvConn);


                if (srvr != null)
                {
                    try
                    {

                        Restore rstDatabase = new Restore();
                        rstDatabase.Action = RestoreActionType.Database;
                        rstDatabase.Database = "taban";
                        OpenFileDialog opfd = new OpenFileDialog();
                        opfd.Filter = "BackUp File|*.taban";
                        if (opfd.ShowDialog() == DialogResult.OK)
                        {


                            BackupDeviceItem bkpDevice = new BackupDeviceItem(opfd.FileName, DeviceType.File);

                            rstDatabase.Devices.Add(bkpDevice);
                            rstDatabase.ReplaceDatabase = true;
                            rstDatabase.SqlRestore(srvr);
                            MessageBox.Show("Database succefully restored", "Server", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("ERROR: An error ocurred while restoring the database", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

        }
    }
我有一个没有SMO问题的新方法
是否存在包含更多详细信息的innerexception?ie e.InnerExceptionCan't Reproduction-这段代码对我来说很好(SQL Server 2012,Visual Studio 2012)备份问题已经解决。问题在于存储位置中的备份。我选择的桌面存储错误,但现在恢复不起作用,并给出相同的错误؟
string _server=".\server_name";
string _database="database_name";
string _backUpFile="filefullpath";
string cmd= "Sqlcmd - E - S .\\"+_server+" - Q\" BACKUP DATABASE ["+ _database + "] TO DISK = '"+backUpFile+"' \" ";
System.Diagnostics.Process.Start(cmd);