Create SQL Backup with SQLDMO.dll

P

PJHORNSA

Hi all,

I want to create a backup of a database using SQLDMO in C#. But I keep
on getting a exception. If anyone can help me by providing code for
this, it would be appreciated. Thanks in advance...

Code:
+++++++++
try
{
this.Cursor = Cursors.WaitCursor;
SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass();
srv.Connect("server","username","password");

SQLDMO.Backup bak = new SQLDMO.BackupClass();
bak.Devices = bak.Files;
string path = txtPath.Text; //Path where the backup should be
created.
path = "[" + path + "]";
bak.Files = path;
bak.Database = "DatabaseName";
bak.SQLBackup(srv);

MessageBox.Show("Database backed up successfully.", "Backup
Successfull");
this.Cursor = Cursors.Default;
}
catch(Exception err)
{
this.Cursor = Cursors.Default;
MessageBox.Show(err.Message,"Error");
}

Exception
++++++++++

[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open backup
device 'C:\test\BackupName.BAK'. Device error or device off-line. See
SQL Server error log for more details.
[Microsoft][ODBC SQL Server Driver][SQL Server]BACKUP DATABASE is
terminating abnormally.


Paul
 
P

PJHORNSA

Sorry, no need to reply on this post, it was my own stupidity that
caused that exception. The code works perfectly...

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top