检测当前数据库是否已部分还原

Detect current database is partial restored [closed]
2021-06-10
  •  译文(汉语)
  •  原文(英语)

当我需要启动应用程序时,我有一个数据库备份文件.如果数据库已部分还原,我想显示一个警告.

如何在C#中检测/识别当前数据库已以完全或部分模式还原.

速聊1:
"部分还原"是什么意思?
速聊2:
简单恢复对没有流文件的数据库进行建模.technet.microsoft.com/zh-CN/library/ms190388.aspx
解决过程1

如果数据库不是AccessReady / ONLINE,则用户将无法连接到数据库.您与数据库服务器的原始连接确保您连接到系统数据库之一.Master / MSDB / TEMPDB等...之后,您将需要在该系统数据库之一中安装一个存储过程,您可以从应用程序中反复调用该存储过程,让您每隔5秒说一次,以便您是否可以重新访问数据库.您的过程将需要此简单查询

如果存在(从sys.databases中选择TOP 1 1,其中WHERE名称="您的数据库名称"和state_desc ="在线")

SELECT 'DB is ready to Access'

ELSE SELECT'DB尚未准备好访问'

速聊1:
是的,但是我有用例,用户可以连接到数据库.他只是部分恢复了数据库而不删除旧数据库,所以他拥有所有数据,但是在这种情况下我需要显示警告也谢谢
速聊2:
好吧,我不知道可以放置此代码的所有可能性,但是您明白了.每当您期望用户收到警告消息时,都需要在代码中找到一个安全的位置,您可以在该位置进行此类调用以检测DATABASE是否实际就绪.您也可以根据第一次失败所引发的错误来触发此调用.
解决过程2

我已经找到一种解决问题的方法,谢谢大家的帮助!

var restoreType = dbContext.Database.SqlQuery<string>(@"Select restore_type FROM [msdb].[dbo].[restorehistory]").FirstOrDefault<string>();

这样,我将检索数据库的还原类型:-如果restoreType ="D",则表示数据库已完全还原!-否则,如果"F"比我知道该数据库已部分还原,则可以!

如果您使用的是另一种连接,则可以使用EF的DbContext,只需使用T-Sql语句即可.

I have a database backup file when I need to start up the application I want to show a warning if the database is partial restored.

How can I detect/recognize in C# that the current database is restored with full or partial mode.

Talk1:
What do you mean by "partial restored"?
Talk2:
Simple Recovery Model the database without the streaming files. technet.microsoft.com/en-us/library/ms190388.aspx
Solutions1

user will not be able to connect to the DB if the DB is is not AccessReady/ONLINE. the oroginal connection you make to the Database server make sure you connect to the one of the system DB. Master/MSDB/TEMPDB etc... after that you will need a stored procedure installed in one of this system DB that you can call from Application repetedly lets say every 5 second that gives you the resut if your DB is reday to access or not. your procedure will need this simple query

IF EXISTS( SELECT TOP 1 1 FROM sys.databases WHERE name= 'your DB name' AND state_desc = 'Online' )

SELECT 'DB is ready to Access'

ELSE SELECT 'DB is not ready to Access'

Talk1:
Yes but I have the use case the user can connect to the database. he just have partially restored the database without to delete the old one so he have all the data but i need in this case to show the warning also thanks
Talk2:
Well I am not aware of all possibilities where you can put this code but you got the point. whenever you are expecting user to receive the warning message you need to find a safe place in your code where you can make such call to detect if DATABASE is actually ready. You can also trigger this call based on the error you got from first failure.
Solutions2

I have found a way to solve the problem thanks for all help!

var restoreType = dbContext.Database.SqlQuery<string>(@"Select restore_type FROM [msdb].[dbo].[restorehistory]").FirstOrDefault<string>();

In this way I will retrive the database restore type: - If restoreType = "D" than database is full restored! - Else if "F" than I know that the database is partial restored!

This DbContext from EF if you are using another kind of connection you can just use the T-Sql statement.

转载于:https://stackoverflow.com/questions/19096013/detect-current-database-is-partial-restored

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入