Skip to main content

Posts

Showing posts from February, 2015

Easy way to parse SQL data reader objects

Many a times, I have seen that people end up writing lot of codes to read from SQL data reader or there is no fail safe mechanism to handle, if parse fails out. In this article, I will try to cover up above issue and implement uniform way to handle things with fail safe mechanism using TryParse approach. Mainly, I will extend inbuilt try parse mechanism comes with .Net. Int.TryParse and similar type of methods was introduced in 2.0 Framework, so the code should be compatible with 2.0 or higher framework. Let's directly dive into usage since we have just single function DbTryParse. I will be explaining function on later stage. We can use with fail safe mechanism where some values are not properly parsed then row will get skipped or in normal way, we define some default value and move ahead with other rows. int id; string val; double dbl; reader["id"].DbTryParse(out id, int.TryParse, 89); reader["val"].DbTryParse(out val, "NA"