使用SMO从SQL Server 2008 R2数据库进行备份无效:
BACKUP DATABASE
异常终止 Source = .Net SqlClient数据提供者
错误码= -2146232060
类= 16
LineNumber上= 1
数= 3201
过程= “”
服务器=(当地)
州= 1堆栈跟踪:
InnerException:Microsoft.SqlServer.Management.Common.ExecutionFailureException
Message =执行Transact-SQL语句或批处理时发生异常 Source = Microsoft.SqlServer.ConnectionInfo堆栈跟踪:
在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand,ExecutionTypes executionType)
在Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands,ExecutionTypes executionType)
在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 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);
}
}
}
}
}
答案 0 :(得分:0)
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);