2013-07-26 65 views
0

我想比較兩個查詢結果以查看它們是否相同。如果結果相同,我想顯示一個MessageBox;如果不做別的。如何比較兩個查詢的結果?

using (OracleCommand crtCommand = new OracleCommand(query1, conn1)) 
{ 
    string result1 = crtCommand.ExecuteScalar().ToString(); 
} 

using (OracleCommand ctCommand = new OracleCommand("query2", conn1)) 
{ 
    string result2 = ctCommand.ExecuteScalar().ToString(); 
} 

if (result1 == result2) 
    MessageBox.Show("They are same"); 
else 
    MessageBox.Show("They are not same"); 
+0

問題是什麼? – SLaks

回答

3

您需要聲明using語句之外的變量,以便它們在該範圍之外可見。

+0

如果我在使用語句之外聲明變量,它們將是可見的,但是我的ctCommand和crtCommand不會是可見的...... – user2619275

+0

只需在using語句之外聲明它們......您仍然可以爲它們賦值_inside_ using語句。 –

0

如果兩個查詢都返回字符串輸出,則可以使用string.Compare方法比較它們並根據輸出有條件地執行您想要執行的操作。然而,這不是那麼簡單,你問的是什麼。你能否進一步闡述你的問題?

0

您只需要在使用塊之外聲明字符串,但仍然在使用塊內分配它們。

string result1; 
string result2; 

using (OracleCommand crtCommand = new OracleCommand(query1, conn1)) 
{ 
    result1 = crtCommand.ExecuteScalar().ToString(); 
} 

using (OracleCommand ctCommand = new OracleCommand("query2", conn1)) 
{ 
    result2 = ctCommand.ExecuteScalar().ToString(); 
} 

if (result1.Equals(result2)) 
    MessageBox.Show("They are same"); 
else 
    MessageBox.Show("They are not same"); 

此外,我建議您更換您的==.Equals(),如果你願意,你可以設置選項,使其不區分大小寫也因此THISthis被視爲相等

if (result1.Equals(result2, StringComparison.OrdinalIgnoreCase)) 
    MessageBox.Show("They are same"); 
else 
    MessageBox.Show("They are not same");