数据库备份还原功能
This commit is contained in:
commit
26085f9971
1 file changed
+1
-41
+1
-41
@@ -157,49 +157,9 @@ namespace formula_manage.Windows
|
||||
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASS;
|
||||
}
|
||||
SqlConnection connection = new SqlConnection(Connstr_SC);
|
||||
// connection.ConnectionString = Connstr_SC;
|
||||
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("use master kill {0}", list[i].ToString()), connection);
|
||||
command.ExecuteNonQuery();
|
||||
connection.Close();
|
||||
}
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// connection.Close();
|
||||
// }
|
||||
|
||||
// 还原数据库
|
||||
connection.Open();
|
||||
command.CommandText = string.Format("use master restore database {0} from disk = '{1}' with replace", dbName, filePath);
|
||||
|
||||
Reference in new issue
Block a user