利用C#備份和還原sqlserver數據庫時,最好使用master數據庫進行操作,以下是備份和還原的代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
// 連接字符串
private string connectionString = "Data Source=DSF-PC;Initial Catalog=master;User ID=sa;Password=123456";
// 構造函數
public Form1()
{
InitializeComponent();
}
// 備份
private void btnBackup_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "備份數據庫";
saveFileDialog.Filter = "備份文件(*.bak)|*.bak";
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
bool ok = Backup("Test", saveFileDialog.FileName);
if (ok)
{
MessageBox.Show("備份成功");
}
else
{
MessageBox.Show("備份失敗");
}
}
}
// 還原
private void btnRestore_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "還原數據庫";
openFileDialog.Filter = "備份文件(*.bak)|*.bak";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
bool ok = Restore("Test", openFileDialog.FileName);
if (ok)
{
MessageBox.Show("還原成功");
}
else
{
MessageBox.Show("還原失敗");
}
}
}
// 備份數據庫
private bool Backup(string dbName, string filePath)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = connectionString;
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = string.Format("backup database {0} to disk = '{1}'", dbName, filePath);
try
{
command.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
connection.Close();
}
}
// 還原數據庫
private bool Restore(string dbName, string filePath)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = connectionString;
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = string.Format("select spid from sysprocesses,sysdatabases where sysprocesses.dbid=sysdatabases.dbid and sysdatabases.Name='{0}'", dbName);
// 獲取當前所有連接進程
List<short> list = new List<short>();
try
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
list.Add(reader.GetInt16(0));
}
reader.Close();
}
catch
{
return false;
}
finally
{
connection.Close();
}
// 殺死當前所有連接進程
try
{
for (int i = 0; i < list.Count; i++)
{
connection.Open();
command = new SqlCommand(string.Format("kill {0}", list[i].ToString()), connection);
command.ExecuteNonQuery();
connection.Close();
}
}
catch
{
return false;
}
finally
{
connection.Close();
}
// 還原數據庫
connection.Open();
command.CommandText = string.Format("restore database {0} from disk = '{1}' with replace", dbName, filePath);
try
{
command.ExecuteNonQuery();
}
catch
{
return false;
}
finally
{
connection.Close();
}
return true;
}
}
}