Hi,
You can create a command and execute the "BACKUP DATABASE". Take a look at the following sample:
System.Data.SqlClient.SqlConnection connection = null;
try
{
connection = new System.Data.SqlClient.SqlConnection();
connection.ConnectionString = "Server=<server>;Initial Catalog=<db>;Integrated Security=SSPI; User Id=<user>;Password=<password>";
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
command.CommandText = @"BACKUP DATABASE <db> TO DISK='C:\filename.bak'";
command.Connection = connection;
connection.Open();
command.ExecuteNonQuery();
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
finally
{
if ((connection != null) && (connection.State != System.Data.ConnectionState.Closed))
{
connection.Close();
connection.Dispose();
}
}
Regards.
|