关于C#写数据库备份的覆盖还原问题
string rebackup = "use master RESTORE DATABASE db_SMS FROM DISK='F\db_SMS.bak' with replace";
try
{
SqlConnection.ClearAllPools();//清空连接池
//杀掉数据库连接的进程
string connection = "server=.;database=master;uid=sa;pwd=123456;Trusted_Connection=False";
SqlConnection conn = new SqlConnection(connection);
conn.Open();
SqlCommand killCmd = new SqlCommand("use master; SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='db_SMS' ", conn);
SqlDataReader myreader=killCmd .ExecuteReader ();
ArrayList list = new ArrayList();
while (myreader.Read())
{
list.Add(myreader.GetInt16(0));
}
myreader.Close();
for (int i = 0; i < list.Count; i++)
{
killCmd = new SqlCommand(string.Format("kill {0}", list), conn);
killCmd.ExecuteNonQuery();
}
if (dbll.DataBackupInfo(rebackup) != 0)
{
SqlConnection.ClearAllPools();//清空连接池
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('还原成功!');", true);
bind();
}
}
catch (Exception)
{
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('还原失败!');", true);
}
dbll.DataBackupInfo(rebackup)是我调用三层的方法
public int DataBackupInfo(string strWhere)
{
return DbHelperSQL.ExecuteSql(strWhere);
}
问题是为什么用上述代码实现数据库备份的覆盖还原为什么要重启下sql才能还原成功,或者是我从未打开过sql server也能还原成功,只要我把sql server中的数据改动或直接把表打开,就会抛出错误:执行killCmd.ExecuteNonQuery()时 提示.附件有语法错误。我实在不明白这到底是为什么,问题该怎么解决,等待高手帮忙