简体   繁体   English

备份失败,“正在使用数据库”

[英]Backup fails, “database is in use”

Backup: 备份:

private void Button1_Click(object sender, EventArgs e)
{
   SqlCommand cmd = new SqlCommand("backup database emp to disk ='C:\\emp.bak'", con);
    con.Open();
    cmd.ExecuteNonQuery();
    // ExecutenonQuery();
    con.Close();
}

But when I restore: 但是当我恢复时:

private void Button2_Click(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand("restore database emp from  disk ='C:\\emp.bak'", con);
    con.Open();
    cmd.ExecuteNonQuery();
    // ExecutenonQuery();
    con.Close();
}

I get the error: 我得到错误:

RESTORE cannot process database 'emp' because it is in use by this session. RESTORE无法处理数据库“ emp”,因为此会话正在使用它。 It is recommended that the master database be used when performing this operation. 建议执行此操作时使用master数据库。 RESTORE DATABASE is terminating abnormally. RESTORE DATABASE异常终止。

Anyone can help me please 任何人都可以帮助我

May be you should use the master database as it suggest. 可能是您应该使用建议的master数据库。

Why don't you use the SMO library ? 为什么不使用SMO库? down is an examples of how to backup and restore database using SMO. 下来是如何使用SMO备份和还原数据库的示例。

How to do a Backup 如何做备份

Server server = new Server(sqlServer);

var bdi = new BackupDeviceItem(String.Format(@"C:\Opticien\Data\Sauvegardes\{0}", backupName), DeviceType.File);
var backup = new Backup() { Database = databaseName, Initialize = true };
backup.Devices.Add(bdi);

backup.SqlBackup(server);

How to restore : 如何还原:

var server = new Server(sqlServer);
var restore = new Restore()
{
     Database = database,
     Action = RestoreActionType.Database,
     ReplaceDatabase = true,
};
restore.Devices.AddDevice(backupPath, DeviceType.File);
var dt = restore.ReadFileList(server);
var dLogicalName = dt.Rows[0]["LogicalName"].ToString();

restore.RelocateFiles.Add(new RelocateFile(dLogicalName, String.Format(@"C:\Opticien\Data\{0}.mdf", database)));
restore.RelocateFiles.Add(new RelocateFile(dLogicalName + "_Log", String.Format(@"C:\Opticien\Data\{0}.ldf", database)));

restore.SqlRestore(server);

Good luck 祝好运

Normally you really want your SQL Database to be in single user mode when you issue a restore command so that you can guarantee that other processes aren't using the same database when this is happening. 通常,发出还原命令时,您确实希望您的SQL数据库处于单用户模式,以便可以保证其他进程在使用同一数据库时不会使用同一数据库。 If anyone else is using the DB at the time you will get a error like you are currently seeing. 如果其他人当时正在使用数据库,则将收到与当前所看到的错误消息。

Have a look at the Single User documentation 看看单用户文档

You might want to consider kill the current sessions before restoring: 您可能需要考虑在恢复之前终止当前会话:

use master
ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE 

--do you stuff here 

ALTER DATABASE YourDatabase SET MULTI_USER

For more details: 更多细节:

How do you kill all current connections to a SQL Server 2005 database? 如何终止当前与SQL Server 2005数据库的所有连接?

When you wanted to restore SQL Databse, you make sure any process doesn't using this database. 当您想要还原SQL Databse时,请确保没有任何进程使用此数据库。 Its better used another name to restore Databse. 最好使用另一个名称来恢复Databse。 I edit my answer: 我编辑我的答案:

DECLARE @SPId int
DECLARE @SQL nvarchar(100)

--SET @DatabaseName = N'AdventureWorks2008'
SET @DatabaseName = DB_NAME()
DECLARE my_cursor CURSOR FAST_FORWARD FOR
SELECT SPId FROM MASTER..SysProcesses
WHERE DBId = DB_ID(@DatabaseName) AND SPId <> @@SPId

OPEN my_cursor

FETCH NEXT FROM my_cursor INTO @SPId

WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'KILL ' + CAST(@SPId as nvarchar(10))
print @SQL
EXEC sp_executeSQL @SQL
--KILL @SPId -- Causing Incorrect syntax near '@spid'.

FETCH NEXT FROM my_cursor INTO @SPId
END

CLOSE my_cursor
DEALLOCATE my_cursor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM