DBCC CheckDb-vb.net 有什么检测错误的方法吗?

2021-09-16 00:00:00 vb.net sql-server dbcc

我正在使用以下代码检查我的数据库是否有任何问题/需要进行故障排除:

I am using the below code to check if my database has any issues/requires troubleshooting:

Dim cmd As New SqlCommand("DBCC CHECKDB (offpoDb) WITH TABLERESULTS", con)
Dim reader As SqlDataReader = cmd.ExecuteReader
executing.Content = "Checking datatabse for errors"
executing.Margin = New Thickness(243, 111, 0, 0)
While reader.Read
    strBuilder.AppendLine(CStr(reader("MessageText")))
End While
reader.Close()
MessageBox.Show(strBuilder.ToString)

现在,DBCC CHECKDB 命令可能会产生如下结果:

Now, the DBCC CHECKDB command might result in something like this :

CHECKDB 在数据库mydb"中发现 0 个分配错误和 15 个一致性错误

可以修复以下 SQL 查询:

Which can be fixed the following SQL query :

ALTER DATABASE AdventureWorks2008R2 SET SINGLE_USER WITH ROLLBACK 
IMMEDIATE;
BEGIN TRANSACTION;
DBCC CHECKDB ('AdventureWorks2008R2', REPAIR_ALLOW_DATA_LOSS);
ALTER DATABASE AdventureWorks2008R2 SET MULTI_USER;

但是在通过我的应用程序执行以下查询之前,有没有办法知道 DBCC CHECKDB 是否真的返回了任何错误?因为没有任何错误,修复数据库就没用了...

But before executing the following query through my application, is there a way to know if DBCC CHECKDB has really returned any errors at all ? Because without any errors, repairing the database will be useless...

有什么帮助吗?

一个让我心碎的想法

我正在考虑将字符串从 strBuilder 获取到文本框.将检查文本框是否可用以下行 CHECKDB 在数据库中发现 0 个分配错误和 15 个一致性错误...

I was thinking of getting the string from strBuilder to a textbox.The textbox will be checked for the availability of the following line CHECKDB found 0 allocation errors and 15 consistency errors in database..

但这仍然是不可能的,因为该行可能会不时不同.例如

But this is still not possible because that line can be different from time to time.E.g.

CHECKDB 发现 0 个分配错误和 15 个一致性错误

CHECKDB 发现 1 个分配错误和 14 个一致性错误

有更好的想法吗?

推荐答案

有多种可能性:

1.
添加NO_INFOMSGS 命令的选项.
这样,如果没有发现错误,就不会返回任何记录.

1.
Add NO_INFOMSGS option to the command.
Like this, no records will be returned if no errors are found.

DBCC CHECKDB (dname) WITH TABLERESULTS, NO_INFOMSGS

<小时>

2.
读取Level列的值.如果大于10,则发生错误.(参考)
您可以获得的 DBCC 错误列表:


2.
Read the value(s) of column Level. If one is higher than 10, an error occured. (Reference)
A list of the DBCC-errors you can get with:

SELECT * FROM sys.sysmessages 
WHERE description LIKE '%checkdb%' AND msglangid = 1033 AND severity > 10

<小时>

3.
检查结果字符串中是否有大于 0 的数字.

For Each c As Char In "CHECKDB found 0 allocation errors and 15 consistency errors"
    If (Char.IsNumber(c)) AndAlso Integer.Parse(c) > 0 Then
        'Errors occured
    End If
Next

相关文章