惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Register - Security
The Register - Security
P
Privacy International News Feed
月光博客
月光博客
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Vercel News
Vercel News
Last Week in AI
Last Week in AI
A
About on SuperTechFans
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
T
Tailwind CSS Blog
GbyAI
GbyAI
S
Schneier on Security
L
LINUX DO - 热门话题
C
CERT Recently Published Vulnerability Notes
AWS News Blog
AWS News Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Hacker News
The Hacker News
N
News and Events Feed by Topic
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
I
Intezer
V2EX - 技术
V2EX - 技术
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
V2EX
IT之家
IT之家
S
SegmentFault 最新的问题
爱范儿
爱范儿
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
S
Security Affairs
Security Latest
Security Latest
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
S
Secure Thoughts
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed

博客园 - Jun1st

Application之间共享Master Page ASP.NET AJAX 4的Client-Side Template和DataView ASP.NET AJAX 4的Client-Side Template和DataView 体验ASP.NET4之ClientID 体验ASP.NET 4之URL Routing Custom WCF Configuration File USE HttpRuntime.Cache OVER HttpContext.Current.Cache Make Asynchronous Calls from Page IIS and VS Embedded Local Web Server Integrate jQuery with ASP.NET Data Controls Tech-Ed 2008 上海 ASP.NET MVC的分页和导航 LINQ and Pipeline Pattern Why Would a .Net Programmer Learn Ruby On Rails(翻译) ASP.NET MVC之AJAX Asp.Net MVC---Walkthrough Asp.Net MVC 入门篇——Overview Asp.Net 2.0之SqlCacheDependency Security Basics and ASP.NET Support(翻译)
使用Extension Methods来使IDataReader更加方便
Jun1st · 2009-08-25 · via 博客园 - Jun1st

2009-08-25 22:09  Jun1st  阅读(2325)  评论()    收藏  举报

今天在逛老赵的Blog的时候,看到了他的常用辅助方法收集一文,想到了自己之前看到的并应用在项目中的一个方法,响应老赵的号召,拿出来晒晒。

在使用DataReader读取数据时,通常会遇到数据可能为Null, 但是又需要转换为如int等其它类型的数据,因此就通常会写这样的代码:

int count = reader["count"] == null ? 0 : int.Parse(reader["count"] + string.Empty);

而且这样的代码通常会大段大段出现在很多地方。

对于比较懒惰的程序员来说,这样的RY(repeat yourself)是一件很痛苦的事情,因此是时候来想个更好的方法了。这里首先想到的就是Extension Method。

SafeRead代码

public static T SafeRead<T>(this IDataReader reader, string fieldName, T defaultValue)
{
    try
    {
        int ordinal = reader.GetOrdinal(fieldName);
        if (reader.IsDBNull(ordinal))//if column "fieldName" doesn't exist, will throw an exception
            return defaultValue;

        return (T)Convert.ChangeType(reader[ordinal], defaultValue.GetType());
    }
    catch(InvalidCastException)
    {
        return defaultValue;
    }
}

很简单的几句代码,有了这个方法之后,在使用DataReader时,就可以通过SafeRead来读取数据了

int count2 = reader.SafeRead<int>("count", 0);

简单但是实用的代码,希望对大家在项目中有所帮助。

最后附上老赵的常用辅助方法收集