DataBase_backup 、Restore and Delete
1
//备份MyBakDb.bak数据库
2
string MyFileName = Application.StartupPath + "\\MyBakDb.bak"; ;
3
string MyDatabase = "myTest";
4
string MySQL= "use master;backup database @MyDatabase to disk = @MyFileName;";
5
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
6
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
7
MyCommand.Parameters.Add("@MyDatabase", SqlDbType.Char);
8
MyCommand.Parameters["@MyDatabase"].Value = MyDatabase;
9
MyCommand.Parameters.Add("@MyFileName", SqlDbType.Char);
10
MyCommand.Parameters["@MyFileName"].Value = MyFileName;
11
try
12
{
13
MyCommand.Connection.Open();
14
MyCommand.ExecuteNonQuery();
15
MessageBox.Show("成功备份指定数据库", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
16
}
17
catch (Exception ex)
18
{
19
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
20
}
21
finally
22
{
23
MyConnection.Close();
24
}
//备份MyBakDb.bak数据库2
string MyFileName = Application.StartupPath + "\\MyBakDb.bak"; ;3
string MyDatabase = "myTest";4
string MySQL= "use master;backup database @MyDatabase to disk = @MyFileName;";5
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");6
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);7
MyCommand.Parameters.Add("@MyDatabase", SqlDbType.Char);8
MyCommand.Parameters["@MyDatabase"].Value = MyDatabase;9
MyCommand.Parameters.Add("@MyFileName", SqlDbType.Char);10
MyCommand.Parameters["@MyFileName"].Value = MyFileName;11
try12
{13
MyCommand.Connection.Open();14
MyCommand.ExecuteNonQuery();15
MessageBox.Show("成功备份指定数据库", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 16
}17
catch (Exception ex)18
{19
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 20
}21
finally22
{23
MyConnection.Close();24
} 1
//恢复MyTestDb数据库
2
string MyFileName = Application.StartupPath + "\\MyTestDb.bak"; ;
3
string MyDatabase = "myTestDb";
4
string MySQL = "use master;Restore database @MyDatabase From disk = @MyFileName;";
5
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
6
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
7
MyCommand.Parameters.Add("@MyDatabase", SqlDbType.Char);
8
MyCommand.Parameters["@MyDatabase"].Value = MyDatabase;
9
MyCommand.Parameters.Add("@MyFileName", SqlDbType.Char);
10
MyCommand.Parameters["@MyFileName"].Value = MyFileName;
11
try
12
{
13
MyCommand.Connection.Open();
14
MyCommand.ExecuteNonQuery();
15
MessageBox.Show("成功恢复指定数据库", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
16
}
17
catch (Exception ex)
18
{
19
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
20
}
21
finally
22
{
23
MyConnection.Close();
24
}
//恢复MyTestDb数据库2
string MyFileName = Application.StartupPath + "\\MyTestDb.bak"; ;3
string MyDatabase = "myTestDb";4
string MySQL = "use master;Restore database @MyDatabase From disk = @MyFileName;";5
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");6
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);7
MyCommand.Parameters.Add("@MyDatabase", SqlDbType.Char);8
MyCommand.Parameters["@MyDatabase"].Value = MyDatabase;9
MyCommand.Parameters.Add("@MyFileName", SqlDbType.Char);10
MyCommand.Parameters["@MyFileName"].Value = MyFileName;11
try12
{13
MyCommand.Connection.Open();14
MyCommand.ExecuteNonQuery();15
MessageBox.Show("成功恢复指定数据库", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);16
}17
catch (Exception ex)18
{19
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);20
}21
finally22
{23
MyConnection.Close();24
} 1
//删除MyTestDb数据库
2
string MySQL = "use master;DROP database MyTestDb;";
3
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
4
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
5
try
6
{
7
MyCommand.Connection.Open();
8
MyCommand.ExecuteNonQuery();
9
MessageBox.Show("成功删除指定数据库", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
10
}
11
catch (Exception ex)
12
{
13
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
14
}
15
finally
16
{
17
MyConnection.Close();
18
}
//删除MyTestDb数据库2
string MySQL = "use master;DROP database MyTestDb;";3
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");4
SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection); 5
try6
{7
MyCommand.Connection.Open();8
MyCommand.ExecuteNonQuery();9
MessageBox.Show("成功删除指定数据库", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);10
}11
catch (Exception ex)12
{13
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);14
}15
finally16
{17
MyConnection.Close();18
}

浙公网安备 33010602011771号