简体   繁体   中英

IF condition check inside USING method and SqlConnection

I am trying to run data validation, execute some code and pass data from one SQL query to another.

My current code looks like the below:

public string SelectUniqueKeyNumber()
{
    string newList = string.Join(Environment.NewLine, listOfSkus).ToString();
    string key_id;
    string sqlConnectionString = @"someConnectionString";

    using (SqlConnection connection = new SqlConnection(sqlConnectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand("select top 1 KEY_NUMBER from MyTable where QTY_ON_HAND > 0 " + newList + " order by NEWID()", connection);

        SqlDataReader readerKey = command.ExecuteReader();
        readerKey.Read();

        key_id = String.Format(readerKey[0].ToString());
    }

    SelectSkuNumber(key_id);
    return key_id;
}

What I am trying to do is to check if my readerKey.Read() is not returning null value. If it does then stop the process, otherwise continue. I've tried it in the way as shown below:

public string SelectUniqueKeyNumber()
{
    string newList = string.Join(Environment.NewLine, listOfSkus).ToString();
    string key_id;
    string sqlConnectionString = @"someConnectionString";

    using (SqlConnection connection = new SqlConnection(sqlConnectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand("select top 1 KEY_NUMBER from MyTable where QTY_ON_HAND > 0 " + newList + " order by NEWID()", connection);

        SqlDataReader readerKey = command.ExecuteReader();
        readerKey.Read();

        if(readerkey.Read().ToString() == null)
        {
            //--- stop processing
        }
        else
        {
            key_id = String.Format(readerKey[0].ToString());
        }
    }

    SelectSkuNumber(key_id); //---> Then ...(key_id) is not declared value
    return key_id;
}

By doing so, I cannot access and pass data of SelectSkuNumber(key_id) due to: Use of unassigned local variable 'key_id'

Any ideas?

All you need do is assign something to key_id when you declare it, like:

string key_id = null; // not string key_id;

and later, after the using:

if (key_id != null)
{
    SelectSkuNumber(key_id); //---> Then ...(key_id) is not declared value
}
return key_id;

The caller of the function should, of course, know what to do if a null is returned.

To avoid that particular problem, you can assign some value or n null to key_id , eg. key_id = ""; .

But you have some more problem there:

  1. You are prone to SQL injection, you should use Parameters collection of SqlCommand class.

  2. Are you sure you are concatenating your query correctly? Let's suppose

     newList = {"some", "thing"}; 

Then your query would be:

select top 1 KEY_NUMBER 
from MyTable where QTY_ON_HAND > 0
some
thing
order by NEWID()

Which is very, very incorrect to say the least.

  1. if(readerkey.Read().ToString() == null) condition... Read returns bool , which is either true or false , it isn't reference type, so ToString() will never be null , thus the condition will always fail. If you want to check if there was NULL in database you should check:

     if (readerKey.Read() && readerKey["KEY_NUMBER"] == DBNull.Value) 

which first read row, then receives value of column in that row. It uses short-circuiting for the case, where no records are returned.

  1. readerKey.Read(); is unnecessary before the if statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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