2009-11-04 187 views
2

我正在嘗試使用ADO.Net執行DBCC CHECK DB('MyDB),但是如何獲取命令返回的文本?從ADO.Net執行DBCC命令

我曾嘗試以下:

SqlCommand sqlCom = new SqlCommand("DBCC CHECKDB ('MyDB')", sqlCon); 
SqlParameter output = new SqlParameter(); 
output.Direction = System.Data.ParameterDirection.ReturnValue; 
sqlCom.Parameters.Add(output); 
int result = sqlCom.ExecuteNonQuery(); 
Console.WriteLine(output.Value); 

但輸出參數值是空的。

回答

2

也許這可以幫到你:http://mspowershell.blogspot.com/2008/01/dbcc-check-through-adonetps.html

編輯:鏈接將讓你到包含下面的腳本博客文章:

$ScriptName = $myInvocation.MyCommand.Name 
[void][reflection.assembly]::LoadWithPartialName("System.Data.SqlClient") 
$ConnString = "Server=Servername\Instance;Integrated Security=SSPI;Database=DatabaseName;Application Name=$ScriptName" 
$MasterConn = new-object ('System.Data.SqlClient.SqlConnection') $ConnString 
$MasterCmd = new-object System.Data.SqlClient.SqlCommand 
$MasterCmd.Connection = $MasterConn 
$SqlDBCC = "DBCC CHECKDB(master) WITH TABLERESULTS" 
$MasterCmd.CommandText = $SqlDBCC 
$MasterConn.Open() 
$Rset = $MasterCmd.ExecuteReader() 
If ($Rset.HasRows -eq $true) { 
    While ($Rset.Read()) { 
     $line = $Rset["MessageText"] 
     If ($Rset["Level"] -gt 10) { 
      Write-Host $line -backgroundcolor Yellow -foregroundcolor Red 
     } else { 
      Write-Host $line 
     } 
    } 
    $Rset.Close() 
} 
$MasterConn.Close() 
+0

謝謝「WITH TABLERESULTS」使所有的差異。 – jaffa 2009-11-04 10:07:24

+0

這是一個「僅供鏈接」的答案。請將重要部分添加到您的答案中。鏈接可以很快破壞... – 2014-04-14 13:22:08

+0

@StefanSteinegger同意! – Konamiman 2014-04-14 14:36:15