Castle ActiveRecord 中空值处理
来源:互联网 发布:mpi编程手册 pdf 编辑:程序博客网 时间:2024/05/01 09:06
我们经常会遇到某个字段可以为空的情况,以下面的Users这个类为例,Birthday这个字段是允许为空值的。
//
// Generated by ActiveRecord Generator
//
//
using Castle.ActiveRecord;
[ActiveRecord("Users")]
public class Users : ActiveRecordBase
{
private int _logonID;
private string _logonName;
private string _password;
private string _emailAddress;
private System.DateTime _lastLogon;
private bool _available;
private System.DateTime _birthday;
[PrimaryKey(PrimaryKeyType.Native)]
public int LogonID
{
get
{
return this._logonID;
}
set
{
this._logonID = value;
}
}
[Property()]
public string LogonName
{
get
{
return this._logonName;
}
set
{
this._logonName = value;
}
}
[Property()]
public string Password
{
get
{
return this._password;
}
set
{
this._password = value;
}
}
[Property(NotNull=true)]
public string EmailAddress
{
get
{
return this._emailAddress;
}
set
{
this._emailAddress = value;
}
}
[Property(Insert = false)]
public System.DateTime LastLogon
{
get
{
return this._lastLogon;
}
set
{
this._lastLogon = value;
}
}
[Property( Insert=false)]
public bool Available
{
get
{
return this._available;
}
set
{
this._available = value;
}
}
[Property()]
public System.DateTime Birthday
{
get
{
return this._birthday;
}
set
{
this._birthday = value;
}
}
public static void DeleteAll()
{
ActiveRecordBase.DeleteAll(typeof(Users));
}
public static Users[] FindAll()
{
return ((Users[])(ActiveRecordBase.FindAll(typeof(Users))));
}
public static Users Find(int LogonID)
{
return ((Users)(ActiveRecordBase.FindByPrimaryKey(typeof(Users), LogonID)));
}
}
// Generated by ActiveRecord Generator
//
//
using Castle.ActiveRecord;
[ActiveRecord("Users")]
public class Users : ActiveRecordBase
{
private int _logonID;
private string _logonName;
private string _password;
private string _emailAddress;
private System.DateTime _lastLogon;
private bool _available;
private System.DateTime _birthday;
[PrimaryKey(PrimaryKeyType.Native)]
public int LogonID
{
get
{
return this._logonID;
}
set
{
this._logonID = value;
}
}
[Property()]
public string LogonName
{
get
{
return this._logonName;
}
set
{
this._logonName = value;
}
}
[Property()]
public string Password
{
get
{
return this._password;
}
set
{
this._password = value;
}
}
[Property(NotNull=true)]
public string EmailAddress
{
get
{
return this._emailAddress;
}
set
{
this._emailAddress = value;
}
}
[Property(Insert = false)]
public System.DateTime LastLogon
{
get
{
return this._lastLogon;
}
set
{
this._lastLogon = value;
}
}
[Property( Insert=false)]
public bool Available
{
get
{
return this._available;
}
set
{
this._available = value;
}
}
[Property()]
public System.DateTime Birthday
{
get
{
return this._birthday;
}
set
{
this._birthday = value;
}
}
public static void DeleteAll()
{
ActiveRecordBase.DeleteAll(typeof(Users));
}
public static Users[] FindAll()
{
return ((Users[])(ActiveRecordBase.FindAll(typeof(Users))));
}
public static Users Find(int LogonID)
{
return ((Users)(ActiveRecordBase.FindByPrimaryKey(typeof(Users), LogonID)));
}
}
如果不对以上代码做些处理,则会出现以下错误:
SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。
解决方案如下:
private System.DateTime? _birthday;
[Property()]
public System.DateTime? Birthday
{
get
{
return this._birthday;
}
set
{
this._birthday = value;
}
}
[Property()]
public System.DateTime? Birthday
{
get
{
return this._birthday;
}
set
{
this._birthday = value;
}
}
以上代码告诉框架Birthday字段可以为空值,这样框架就能够正确的去处理。当然还可以采用其他的方法比如:使用框架自带的空属性。我个人还是倾向于第一种方法,比较简单。
- Castle ActiveRecord 中空值处理
- [Castle ActiveRecord] 2. ActiveRecord
- DB2中空值处理
- Castle ActiveRecord 学习 123
- Castle ActiveRecord 介绍
- Castle.ActiveRecord初探
- Castle ActiveRecord使用笔记
- Castle ActiveRecord 配置
- [Castle ActiveRecord] 1. Starter
- [Castle ActiveRecord] 3. Validate
- [Castle ActiveRecord ] 4. CRUD
- Castle.ActiveRecord初探
- [Castle ActiveRecord ] 5. Base Relations
- Castle ActiveRecord【快速入门指南】
- Castle ActiveRecord的排序查询
- Castle ActiveRecord 的分页查询
- SQL中空值的的处理:
- SQL Server数据库中空值处理技巧
- 四月,又一个四月
- [转]内存拷贝的优化方法
- 优秀的程序员
- VS2005进行Web调试的浏览器选择更改
- 初识linux
- Castle ActiveRecord 中空值处理
- 开源RTS游戏 Warzone2100(2.1beta2) 编译备忘
- JSP 利用 JFreechart直接生成报表图形的例子
- 解决无耻迅雷在后台偷偷上传文件的一种方法
- Sliverlight 2.0 Beta 离线文档
- C#中命令行参数的应用领域及其实现
- 用ajax实现仿CSDN下载评论的闪烁效果
- C#中将某一文件拖向应用程序后打开或进行其它操作的实现
- C#应用程序的返回值