ParsX.com
پذیرش پروژه از دانشجویی ... تا سازمانی 09376225339
View previous topic :: View next topic
Author
Message
donya_samani مهمون يكي دو روزه Joined: 27 Dec 2011 Posts: 2
Posted: Tue Dec 27, 2011 10:43 pm Post subject: بک آپ گیری و ریستور Backup & Restore در vb.net
سلام یه Application در محیط ویژوال استودیو به زبان vb ایجاد کردم که امکانات حذف و ثبت و ویرایش و جستجو و .. رو در بانک اطلاعاتی sql داره حالا میخوام یک دکمه ی backup و یک دکمه ی Restore هم ایجاد کنم در Application کدی توش بنویسم که عملیات بک آپ گیری (تهیه نسخه پشتیبان) و ریستور (بازیابی اطلاعات) رو انجام بده . چه جوری کدش رو بنویسم ؟
محیط عملیاتی :
ویژوال استودیو 2008 به زبان Vb
جداول بانک اطلاعاتی با Sql2008
Back to top
vahid بي تو هرگز Joined: 26 Nov 2004 Posts: 3067 Location: Tehran
Posted: Sat Dec 31, 2011 5:28 pm Post subject:
private void btnBackup_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.AddExtension = true;
saveFileDialog.CheckFileExists = false;
saveFileDialog.CheckPathExists = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.FileName = "Backup";
saveFileDialog.Filter = "Backup File (*.Bak)|*.Bak";
saveFileDialog.DefaultExt = "Bak";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.InitialDirectory = System.Windows.Forms.Application.StartupPath;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
this.Refresh();
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(GlobalClass.GlobalConnString);
cmd.Connection.Open();
string dbname = cmd.Connection.Database;//
cmd.Connection.Close();
SqlConnection.ClearAllPools();
string query = "BACKUP DATABASE [" + dbname + "] TO DISK ='" + saveFileDialog.FileName + "' WITH NOFORMAT, NOINIT, SKIP, NOREWIND, NOUNLOAD, STATS = 10";
cmd.Connection = new SqlConnection(GlobalClass.GlobalConnString);
cmd.Connection.Open();
cmd.CommandText = query;
cmd.ExecuteNonQuery();
cmd.Connection.Close();
this.Refresh();
MessageBox.Show("پشتيبان گيري به پايان رسيد. نسخه مربوطه را در يك سي دي نگهداري كنيد.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show(" خطا در پشتيبان گيري مجددا تلاش كنيد. ", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnRestore_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.AddExtension = true;
openFileDialog.CheckFileExists = false;
openFileDialog.CheckPathExists = true;
openFileDialog.Filter = "Backup File (*.Bak)|*.Bak";
openFileDialog.DefaultExt = "Bak";
openFileDialog.RestoreDirectory = true;
openFileDialog.InitialDirectory = System.Windows.Forms.Application.StartupPath;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
this.Refresh();
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(GlobalClass.GlobalConnString);
cmd.Connection.Open();
string dbname = cmd.Connection.Database;
cmd.Connection.Close();
string query = "USE master ;ALTER DATABASE [" + dbname + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE ;RESTORE DATABASE [" + dbname + "] FROM DISK = '" + openFileDialog.FileName + "' with replace ;ALTER DATABASE [" + dbname + "] SET MULTI_USER ;";
cmd.Connection = new SqlConnection(GlobalClass.GlobalConnString);
cmd.Connection.Open();
cmd.CommandText = query;
cmd.ExecuteNonQuery();
cmd.Connection.Close();
MessageBox.Show("اطلاعات باموفقيت بازيابي شد.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "خطا در بازيابي اطلاعات مجددا تلاش كنيد", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Back to top
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
s
Powered by phpBB © 2001, 2010 phpBB Group
آمار