Tutorial : Backup and Restore SQLExpress with C#
by toy
SQLExpress 2005 will use SMO to manage database, it ship drom DMO from older version (2000). In this topic I’ll talk about SMO.
If you are using SQLServer Express Edition must read this topic
http://www.codeproject.com/useritems/SMODemo.asp This is example application with SMO.
http://www.sqldbatips.com/showarticle.asp?ID=34 Another example application.
However, I’ll present another way to use Backup and Restore database via C#, not using SMO. We’re using StoreProcedure. Sound wired aha, but I just find another ways HaHa.
CREATE PROCEDURE BackUpDatabase
@name varchar(max)
AS
BACKUP DATABASE databasename TO @name
WITH FORMAT, NAME = ‘Full Database Backup’, SKIP, NOREWIND, NOUNLOAD, STATS = 10
This store procedure can backup database in the same way, but maybe not easy to use it. anyway I prefer to use SMO to backup and restore database, much more easier.
In c#
myConnection = new ….
SqlCommand comadd = new SqlCommand(“BackUpDatabase”,myConnection);
comadd.CommandType = CommandType.StoredProcedure;SqlParameter param1 = new SqlParameter(“@name”, name);
comadd.Parameters.Add(param1);try
{
comadd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
myConnection.Close();
}
Or you can use direct SQL direct to SqlCommand neither, It’s work fine.
Twitter
Facebook