简体   繁体   English

如何比较两个查询的结果?

[英]How can I compare the results of two queries?

I'd like to compare two query results to see if they are the same or not. 我想比较两个查询结果,看它们是否相同。 If the result is the same, I want to display a MessageBox ; 如果结果相同,我想显示一个MessageBox if not do something else. 如果不做其他事情。

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");

您需要在using语句之外声明变量,以便它们在该范围之外可见。

If both your queries return string output you can use string.Compare method to compare them and conditionally do what you want to do, base on output. 如果两个查询都返回字符串输出,则可以使用string.Compare方法比较它们,并根据输出有条件地执行您想做的事情。 However, it can't be that simple what you are asking. 但是,您要问的不是那么简单。 Can you elaborate further your question? 您能否进一步阐述您的问题?

All you need to do is declare the strings outside of the using blocks but still assign them inside the using blocks. 您需要做的就是在using块外声明字符串,但仍在using块内分配它们。

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");

Also I recommend replacing your == with .Equals() , if you want you can set options to make it case insensitive too so THIS and this are considered equal 我也建议您将。 ==替换为.Equals() ,如果您愿意的话,也可以设置选项使其不区分大小写,因此, THISthis相等

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM