你如何处理可空类型具有的SqlDataRecord [英] How do you handle Nullable type with SqlDataRecord

查看:207
本文介绍了你如何处理可空类型具有的SqlDataRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析XML(LINQ到XML)和我使用的是可空类型( INT?小数?)在元素/属性是空的情况。然而,建立我的收藏传递到数据库时(使用TVP)我不知道如何处理的情况下,该值实际上是空。我不能传递一个空入的SqlDataRecord SetInt32或SetDecimal,我不想设置为零....其实我希望它是空。

I am parsing XML (LINQ to XML) and I am using a nullable type (int? and decimal?) in cases where the element / attribute is empty. However, when building my collection to pass to the DB (Using TVP) I don't know how to handle the cases where the value is actually null. I can't pass a null into the SqlDataRecord SetInt32 or SetDecimal and I don't want to set to zero....I actually want it to be null.

告诉我为 INT 无过载?

下面数是可空类型(诠释? 计数)

Count below is a nullable type (int? Count)

SqlDataRecord rec = new SqlDataRecord(
              new SqlMetaData("Name", SqlDbType.VarChar, 50),
              new SqlMetaData("Type", SqlDbType.VarChar, 50),
              new SqlMetaData("Count", SqlDbType.Int));

   rec.SetString(0, dm.Name);
   rec.SetString(1, dm.Type);
   rec.SetString(2, dm.Count);



任何想法如何处理这个问题,而不传递零(保持为空)?

Any ideas how to handle this without passing zero (maintaining the null)?

推荐答案

扩展方法:

static class SqlDataRecordExtensions
{
    static void SetNullableInt32(this SqlDataRecord rec, int index, Int32? value)
    {
        if (value.HasValue)
            rec.SetInt32(index, value.GetValueOrDefault());
        else
            rec.SetDBNull(index);
    }
}

或使用 SetSqlInt32 作为以D士丹利建议:

or, to use SetSqlInt32 as suggested by D Stanley:

static class SqlDataRecordExtensions
{
    static void SetNullableInt32(this SqlDataRecord rec, int index, Int32? value)
    {
        rec.SetSqlInt32(index, value.HasValue ? value.GetValueOrDefault() : SqlInt32.Null);
        //                                      ^^^^^^^^^^^^^^^^^^^^^^^^^
        //                                      You can leave out the cast to (SqlInt32),
        //                                      because the conversion is implicit
    }
}

请注意, 2013年12月9日:回到这个答案,因为一个评论,我发现需要改进的一个小的机会,基于埃里克利珀对可空微优化系列,可在的 http://ericlippert.com/2012/12/20/nullable-micro-optimizations-part-one/

Note, 9 Dec 2013: Returning to this answer because of a comment, I noticed a small opportunity for improvement, based on Eric Lippert's series on nullable micro-optimizations, which can be found at http://ericlippert.com/2012/12/20/nullable-micro-optimizations-part-one/.

在短暂的,而属性需要更少的输入,因此可以说是对程序员更优化,它抛出一个异常,如果HasValue为假。在另一方面, GetValueOrDefault()法是一种简单的现场访问。正因为如此, GetValueOrDefault()需要更少的指令和更可能被内联,所以它是为编译器和处理器更优

In brief, while the Value property requires less typing and is therefore arguably more optimal for the programmer, it has to throw an exception if HasValue is false. On the other hand, the GetValueOrDefault() method is a simple field access. Because of this, GetValueOrDefault() requires fewer instructions and is more likely to be inlined, so it is more optimal for the compiler and the processor.

这篇关于你如何处理可空类型具有的SqlDataRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆