从具有SMO的数据库备份失败

问题描述 投票:0回答:1

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

BACKUP DATABASE正在异常终止。Source = .Net SqlClient数据提供程序错误代码= -2146232060类= 16LineNumber = 1数= 3201程序=“”服务器=(本地)状态= 1

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

StackTrace:在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand,ExecutionTypes执行类型)在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands,ExecutionTypes执行类型)在Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection查询)在Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(服务器服务器,StringCollection查询)在Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)

InnerException:System.Data.SqlClient.SqlException在Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction操作,Object execObject,DataSet fillDataSet,Boolean catchException)在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand,ExecutionTypes executeType)

我的代码:

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);
                    }
                }
            }

        }
    }
c# winforms sql-server-2008-r2 backup smo
1个回答
0
投票

我有没有SMO问题的新方法

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);
© www.soinside.com 2019 - 2024. All rights reserved.