par , 16/03/2016 à 15h05 (3258 Affichages)
Bonjour,
Dans ce rubrique on va créer un fichier de sauvegarde (*.bak) d'une base données Sql Server puis on va faire la restauration en utilisant C#.
Cet exemple est développé sous Visual Studio 2010. On va créer ce WINDOWS FORM:

On va commencer par la tache de Sauvegarde. Pour le bouton Browse on ajoute ce code pour spécifier l'emplacement ou le fichier de sauvegarde doit être créé.
1 2 3 4 5 6 7 8 9
| private void browseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = dlg.SelectedPath;
BackupButton.Enabled = true;
}
} |
Et voila le code de bouton Backup qui va créer le fichier de sauvegarde (*.bak);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| private void BackupButton_Click(object sender, EventArgs e)
{
string database = con.Database.ToString();
try
{
if(textBox1.Text==string.Empty)
{
MessageBox.Show("please enter backup file location");
}
else
{
string cmd = "BACKUP DATABASE [" + database + "] TO DISK='" + textBox1.Text + "\\" + "database" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak'";
using(SqlCommand command = new SqlCommand(cmd,con))
{
if(con.State!=ConnectionState.Open)
{
con.Open();
}
command.ExecuteNonQuery();
con.Close();
MessageBox.Show("database backup done successefully");
BackupButton.Enabled = false;
}
}
}
catch
{
}
} |
Maintenant on va faire la restauration, on ajoute ce code au bouton Browse pour specifier l'empalcement de notre fichier de sauvegarde:
1 2 3 4 5 6 7 8 9 10 11
| private void Browsebutton2_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "SQL SERVER database backup files|*.bak";
dlg.Title = "Database restore";
if (dlg.ShowDialog() == DialogResult.OK)
{
textBox2.Text = dlg.FileName;
restoreButton.Enabled = true;
}
} |
Et enfin voici le code bouton Restore qui fait la restauration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| private void restoreButton_Click(object sender, EventArgs e)
{
string database = con.Database.ToString();
if (con.State != ConnectionState.Open)
{
con.Open();
}
try
{
string sqlStmt2 = string.Format("ALTER DATABASE [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
SqlCommand bu2 = new SqlCommand(sqlStmt2, con);
bu2.ExecuteNonQuery();
string sqlStmt3 = "USE MASTER RESTORE DATABASE [" + database + "] FROM DISK='" + textBox2.Text + "'WITH REPLACE;";
SqlCommand bu3 = new SqlCommand(sqlStmt3, con);
bu3.ExecuteNonQuery();
string sqlStmt4 = string.Format("ALTER DATABASE [" + database + "] SET MULTI_USER");
SqlCommand bu4 = new SqlCommand(sqlStmt4, con);
bu4.ExecuteNonQuery();
MessageBox.Show("database restoration done successefully");
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
} |
Mis à jour 16/03/2016 à 16h35 par kolodz
([Blog]Ajout langage à la balise code)
- Catégories
-
DotNET
,
C#