[此贴子已经被作者于2007-3-28 8:38:29编辑过]
好的。
string str=ajbh.dataserver.Replace("yao","master");//这里是连接字符串ajbh.dataserver,进行数据库替换
SqlConnection conn=new SqlConnection (str);
conn.Open ();
//杀死正在使用的进程
string sql="SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='yao'";
SqlCommand cmd1 = new SqlCommand(sql, conn);
SqlDataReader dr;
ArrayList list = new ArrayList();
try
{
dr = cmd1.ExecuteReader();
while(dr.Read())
{
list.Add(dr.GetInt16(0));
}
dr.Close();
for(int i = 0; i < list.Count; i++)
{
cmd1 = new SqlCommand(string.Format("KILL {0}", list[i].ToString ()), conn);
cmd1.ExecuteNonQuery();
}
conn.Close();
}
catch(SqlException eee)
{
conn.Close();
MessageBox.Show (eee.ToString ());
}
//开始备份
try
{
SqlCommand cm=new SqlCommand();
cm.Connection=conn;
cm.CommandText="RESTORE DATABASE yao FROM disk ='"+this.txtfilename.Text.Trim()+"'";// WITH FORMAT, NAME = 'Full Backup of MyNwind'
conn.Open();
cm.ExecuteNonQuery();
conn.Close();
MessageBox.Show("数据库已经完成恢复!!","提示");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}