简体   繁体   中英

Is there a right way to retrieve a String field in SQL

I have seen different ways to retrieve a string field from SQL. Is there a "right" way, and what are the differences

  SqlDataReader rdr;
  1.  String field = (String) rdr["field"];
  2.  String field = rdr["field"].ToString();
  3.  String field = rdr["field"] As String;

Thanks!

You could also use:

int ordinal=rdr.GetOrdinal("stringField");
if (rdr.IsDBNull(ordinal))
{
    return string.Empty; //Or null or however you want to handle it 
}
else
{   
   rdr.GetString(ordinal);
}

Which if you look at the definition for SQlDataReader["field"] looks like:

public override object this[string name]
{
    get
    {
        return this.GetValue(this.GetOrdinal(name));
    }
}

Essentially this is doing the same thing, only it's type safe. What I like to do is Create my own IDataReader which wraps SqlDataReader. CSLA uses a simillar mechanism that they call SafeDataReader since it provides overloads for all the various data types which implements this pattern.

If you know the field won't be null you could leave out the isDbNull checks. Due to the verbosity I'd recommend putting this in some type of wrapper or helper class and make functions out of them.

  1. May cause an exception if the type is not string
  2. Is definitely wrong because that could raise an exception if the value is null.
  3. Will assign null if it is null or if it is of type other than string

So, to be on the safe side - I would choose 3.

Keep in mind that the question interacts with your data definitions. If "field" has been defined as "Not Null" then you don't have to worry about null data and should choose #1 for readability. Similarly, if the field is nullable but you use an "IsNull" function when doing your query:

Select IsNull(Field1, '') as Field1 From DBTable Where...

then, again, you should choose #1 because you still don't have to worry about null. Of course, this assumes that you would WANT the null value to be masked by the empty string. If you want to test against null because it is an error condition then you'd have logic like:

if (nwReader.IsDBNull(nwReader.GetOrdinal("Field1")))
    *throw exception or otherwise handle null condition
string aStr = (string)nwReader["field"];

This last case, however, is not really good practice. If null is an invalid value - an error condition - then you should rule it out in your DDL.

In the end, though, I always go for option #1 because I think it leads to better readability and it forces me to make my null handling explicit.

If I'm expecting a string, I'll do #1. If for some reason the field isn't a string or changes type, you'll at least know about it through the exception mechanism. (Just don't wrap it inside of a try / empty catch.)

I like the ?? operator for checking nulls (as discussed in other posts)

String field = rdr["field"] As String ?? string.Empty

This should work. If not, well its late :P, unless you actually want the result to be null. If so then I like 3.

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