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

时间:2023-02-19
本文介绍了DBCC CheckDb-vb.net 有什么检测错误的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

这篇关于DBCC CheckDb-vb.net 有什么检测错误的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!