简体   繁体   English

如何检查列是否具有特定值

[英]How to check whether a column has a particular value

I am have a MS SQL database where there are field like: 我有一个MS SQL数据库,其中有如下字段:

  • refno 参考编号
  • businessid 商业ID
  • worker 工人
  • locationname 地点名称
  • time 时间
  • description 描述
  • quotation 报价单

With same businessid there can be many rows and with same refno there can be many rows. 具有相同businessid的行可以有很多行,具有相同refno的行可以有很多行。 But with same refno and businessid there can be only one row. 但是,使用相同的refno和businessid只能有一行。 Now there can be many rows with same businessid, but I want to check whether a particular refno exist in those businessid or not. 现在可以有许多具有相同businessid的行,但是我想检查在这些businessid中是否存在特定的refno。 I know how to check whether a businessid exist in a table or not. 我知道如何检查表中是否存在businessid。 But I am confused how to check that businessid with same name have a particular refno or not. 但是我很困惑如何检查同名的businessid是否具有特定的refno。

string g = Session["businessid"].ToString();
string h = Session["referenceno"].ToString();
con.Open();
SqlCommand check = new SqlCommand("select * from quoted_price where businessid=@businessid", con);
check.Parameters.AddWithValue("@businessid", g);
SqlDataReader dr = check.ExecuteReader();        
if (dr.HasRows)
{
    con.Close();
}

You've one major problem with your code. 您的代码有一个主要问题。 You aren't properly handling objects that implement IDisposable. 您没有正确处理实现IDisposable的对象。 You need to either use a using block or the try/catch/finally pattern, where you handle cleanup in the finally block. 您需要使用using块,也可以使用try / catch / finally模式,在finally块中处理清理。 Failure to do so can lead to connections that get left open, and you can run into strange hard to diagnose errors later on. 否则,连接可能会断开,随后您可能会遇到奇怪的难以诊断的错误。 For any class you use (particularly when storage or network access is required), always check if it implements IDisposable . 对于您使用的任何类(尤其是在需要存储或网络访问的情况下),请始终检查其是否实现IDisposable

You also should think about when you create your SqlConnection vs when you open your connection. 您还应该考虑何时创建SqlConnection与何时打开连接。 Why open the connection prior to being ready to use it? 为什么在准备使用连接之前先打开连接?

Also, variables should adequately describe the information they contain. 同样,变量应充分描述它们包含的信息。 Avoid using unhelpful names like g and h . 避免使用无用的名称,例如gh

And lastly, you can use two parameters to check the condition you wish to check. 最后,您可以使用两个参数来检查要检查的条件。

string businessId = Session["businessid"].ToString();
string referenceNo = Session["referenceno"].ToString();    
bool hasRows = false;    

using(SqlConnection connection = new SqlConnection(parameters))
{
    using(SqlCommand checkCommand = new SqlCommand("select * from quoted_price where businessid=@businessid AND refno=@refno", connection))
    {
        checkCommand.Parameters.AddWithValue("@businessid", businessId);
        checkCommand.Parameters.AddWithValue("@refno", referenceNo);
        connection.Open();
        using(SqlDataReader dataReader = check.ExecuteReader())
        {
             hasRows = dataReader.HasRows;
        }                   
    }
}

Notice how instead of attempting to do a lot of logic with the dataReader that I just get what I want from it and store the value I care about. 请注意,我没有尝试对dataReader进行大量逻辑处理,而只是从中获取想要的东西并存储了我关心的值。 This is because I don't want to leave the connection open any longer than necessary. 这是因为我不想让连接保持打开状态的时间超过了必要。

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

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