Click here to Skip to main content
15,889,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can backup a database on hard disk with the following method :

void BackUp(string ConnectionString, string DatabaseFullPath, string backUpPath)
{
    progressBar1.Value = 0;

    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        con.Open();

        string UseMaster = "USE master";
        SqlCommand UseMasterCommand = new SqlCommand(UseMaster, con);
        UseMasterCommand.ExecuteNonQuery();

        progressBar1.Value += 25;

        string Alter1 = @"ALTER DATABASE [" + DatabaseFullPath + "] SET Single_User WITH Rollback Immediate";
        SqlCommand Alter1Cmd = new SqlCommand(Alter1, con);
        Alter1Cmd.ExecuteNonQuery();

        progressBar1.Value += 25;

        string Restore = @"BACKUP DATABASE [" + DatabaseFullPath + "] TO  DISK = N'" + backUpPath + @"' WITH NOFORMAT, NOINIT,  NAME = N'" + DatabaseFullPath + "-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10";
        SqlCommand RestoreCmd = new SqlCommand(Restore, con);
        RestoreCmd.ExecuteNonQuery();

        progressBar1.Value += 25;

        string Alter2 = @"ALTER DATABASE [" + DatabaseFullPath + "] SET Multi_User";
        SqlCommand Alter2Cmd = new SqlCommand(Alter2, con);
        Alter2Cmd.ExecuteNonQuery();

        progressBar1.Value += 25;

        labelReport.Text = "Successful";
    }
}


and , How can I convert it to a Stored Procedure ?

(I'm newbie in MS-SQL)
Posted
Updated 18-Nov-10 20:36pm
v2

1 solution

While debugging this code, put break points in the code and move whatever query you excute in the commands to a stored procedure.

For each of the parameters in the query, create a paramter in the stored procedure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900