DBCC PAGE
命令能够通过数据库或日志文件(如
DBCC TRACEON(3604)
DBCC PAGE('TESTDB',2,0,2) WITH TABLERESULTS
但是它经常包含服务器错误或警告。 SSMS能够正确显示它,有“结果”和“消息”选项卡。我正试图用C#
获得结果var conn = new SqlConnection("Data Source=PCNAME;Initial Catalog=TESTDB;User ID=user;Password=password");
SqlCommand cmdPageNo = new SqlCommand();
SqlDataReader readerPageNo, readerPage = null;
cmdPageNo.CommandText = "SELECT fileid, size FROM sysfiles where name = 'TESTDB_log'";
cmdPageNo.CommandType = CommandType.Text;
cmdPageNo.Connection = conn;
conn.Open();
readerPageNo = cmdPageNo.ExecuteReader();
if (readerPageNo.Read()) {
int id = Convert.ToInt32(readerPageNo.GetValue(0));
int pageCnt = Convert.ToInt32(readerPageNo.GetValue(1));
readerPageNo.Close();
SqlCommand cmdNQ = new SqlCommand("DBCC TRACEON(3604)", conn);
cmdNQ.ExecuteNonQuery();
SqlCommand cmdPage = new SqlCommand("DBCC PAGE('TESTDB', @fileid, @pageno, 2) WITH TABLERESULTS", conn);
SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@fileid";
param1.Value = id;
cmdPage.Parameters.Add(param1);
SqlParameter param2 = new SqlParameter();
param2.ParameterName = "@pageno";
cmdPage.Parameters.Add(param2);
for (int i = 0; i < pageCnt; i++)
{
try
{
param2.Value = i;
try
{
readerPage = cmdPage.ExecuteReader();
}
catch
{
// there are always errors and warnings
}
}
finally
{
if (readerPage != null)
{
string temp = readerPage.GetString(0);
}
}
}
}
但readerPage
部分中的错误finally
为空后。我检查了SQL Server发送的数据包:在几条消息(有些消息导致异常的SeverityClass 16)之后,有字段定义和值。问题是如何忽略服务器错误消息引起的错误并从SSMS等数据集中获取数据?