zoukankan      html  css  js  c++  java
  • 使用新类型Nullable处理数据库表中null字段

    .net 2.0中,提供了 Nullable的范型,通过它,我们可以为基础类型如int等赋予null的值,这样我们就可以处理null值了。

    例子代码

     数据表有个字段updateTimestamp,可以为null值。在实体类中使用如下设置:

     

            private DateTime? _updateTimestamp;

     

          /// <summary>

            /// 文件更新日期

            /// </summary>

            public Nullable<DateTime> UpdateTimestamp

            {

                get { return this._updateTimestamp; }

                set { this._updateTimestamp = value; }

     }

     

           /// <summary>

            /// 从DataReader中加载数据

            /// </summary>

            /// <param name="rdr"></param>

            public void Load(IDataReader rdr)

            {

                if (rdr.Read())

                {

                    IsLoaded = true;

                    this.FileId = (int)rdr["fileId"];

                    if (!rdr["updatetimestamp"].Equals(DBNull.Value))

                    {

                        this.UpdateTimestamp = (DateTime)rdr["updatetimestamp"];

                    }

                    ……

                    }

                }

    }

     

    //保存文件方法

    public abstract int CreateFile(……,,DateTime? updatetimestamp, int downloadCount);

     

    获取Nullable字段的值

            this.calDatePublished.SelectedDate = this.file. UpdateTimestamp.Value;

            不能直接使用this.calDatePublished.SelectedDate = this.file. UpdateTimestamp;


     

    参考:http://blogs.msdn.com/ericgu/archive/2004/05/27/143221.aspx

    欢迎大家扫描下面二维码成为我的客户,为你服务和上云

  • 相关阅读:
    设计模式——装饰模式(Decorator Pattern)
    设计模式——策略模式(Strategy Pattern)
    设计模式——简单工厂模式(SimpleFactory Pattern)
    入博客园三周年记
    android+Service
    surfaceView+canvas+paint+bitmap
    Enable Sublime text 2 to support GBK in Mac
    androidstudio+opencv
    mac下的环境变量PATH
    Curl命令
  • 原文地址:https://www.cnblogs.com/shanyou/p/314904.html
Copyright © 2011-2022 走看看