我要修改 ID=3 的那一条记录,
下面这段代码执行的时候没有错:private void Button2_Click(object sender, EventArgs e)
{
Configuration cfg = new Configuration();
cfg.AddXmlFile(Server.MapPath("NHEntity/userinfo.hbm.xml"));
ISession iS = cfg.BuildSessionFactory().OpenSession();
ITransaction iT = iS.BeginTransaction();
UserInfo ui = (UserInfo) iS.Load(typeof(UserInfo), 3);
ui.Name = "August";
ui.Sex = false;
ui.Hit = 88;
ui.Birthday = DateTime.Parse("1983-10-1");
iS.Update(ui);
iT.Commit();
iS.Close();
}但是,我又想把“Birthday”字段修改成“null”值?
应该如何做?

解决方案 »

  1.   

    tryui.Birthday = DateTime.MinValue;
      

  2.   

    SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 
     
    异常详细信息: System.Data.SqlTypes.SqlTypeException: SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。
      

  3.   

    make sure the birthday field in your database table allows null
      

  4.   

    CREATE TABLE [userinfo] (
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [sex] [bit] NULL CONSTRAINT [DF_userinfo_sex] DEFAULT (1),
    [hit] [int] NULL CONSTRAINT [DF_userinfo_hit] DEFAULT (0),
    [birthday] [smalldatetime] NULL ,
    [regdate] [smalldatetime] NULL CONSTRAINT [DF_userinfo_regdate] DEFAULT (getdate()),
    CONSTRAINT [PK_userinfo] PRIMARY KEY  CLUSTERED 
    (
    [id]
    )  ON [PRIMARY] 
    ) ON [PRIMARY]
    GO
      

  5.   

    a little odd, what if you change smalldatetime to datetime?
      

  6.   

    改成了datetime类型的,还是这个提示:
    SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlTypes.SqlTypeException: SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。
      

  7.   

    问题已解决:
    http://jeky.cnblogs.com/archive/2005/12/28/306424.html
      

  8.   

    using string is not a good solution
      

  9.   

    仅仅是表实体及xml映射是 String 类型的,
    库中的类型仍是 DateTime 的。类型不一致,看起来是有点不太舒服,但目前已找不到好一点的解决方案了。
      

  10.   

    在SQL SERVER 2000上无法重复你的问题,参考(编码有点重复,但纯为示范目的)1. sql
    CREATE TABLE [userinfo] (
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [sex] [bit] NULL CONSTRAINT [DF_userinfo_sex] DEFAULT (1),
    [hit] [int] NULL CONSTRAINT [DF_userinfo_hit] DEFAULT (0),
    [birthday] [smalldatetime] NULL ,
    [regdate] [smalldatetime] NULL CONSTRAINT [DF_userinfo_regdate] DEFAULT (getdate()),
    CONSTRAINT [PK_userinfo] PRIMARY KEY  CLUSTERED 
    (
    [id]
    )  ON [PRIMARY] 
    ) ON [PRIMARY]
    GO
    2. UserInfo.cs (compiled into UserInfo.dll)
    using System;
    //note, in the Quick Guide, it says NHibernate.Demo.QuickStart
    //you could use it, then you need to change the class name in 
    //the User.hbm.xml file
    namespace NHibernate.Examples.QuickStart
    {
     public class UserInfo
     {
      private int id;
      private string name;
      private bool sex;
      private int hit;
      private DateTime birthday;
      private DateTime regdate;
      public UserInfo()
      {
      }
      public int Id 
      {
       get { return id; }
       set { id = value; }
      }
      public string Name 
      {
       get { return name; }
       set { name = value; }
      }
      public bool Sex
      {
       get { return sex; }
       set { sex = value; }
      }
      public int Hit
      {
       get { return hit; }
       set { hit = value; }
      }
      public DateTime Birthday 
      {
       get { return birthday; }
       set { birthday = value; }
      }
      
      public DateTime RegDate
      {
        get { return regdate;}
        set { regdate= value;}
      }
     }
    }
    3. UserInfo.hbm.xml
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
     <class name="NHibernate.Examples.QuickStart.UserInfo, UserInfo" table="userinfo">
      <id name="Id" column="Id" type="Int32"> 
       <generator class="native" /> 
      </id> 
      <property name="Name" column= "name" type="String" length="50"/> 
      <property name="Sex" type="Boolean"/> 
      <property name="Hit" type="Int32"/>
      <property name="Birthday" type="DateTime"/>
      <property name="RegDate" type="DateTime"/>
     </class>
    </hibernate-mapping>4. TestUser.cs
    using System;
    //using System.Configurations;
    using NHibernate;
    using NHibernate.Cfg;
    using System.Text;
    using System.Collections;
    using NHibernate.Examples.QuickStart;
    using NHibernate.Expression;
    class TestConfig
    {
      public static void Main()
      {
      int id = Create();
    Select(id);
    Update(id,true);
    Console.WriteLine("enter to continue");Console.ReadLine();
    Select(id);
    Update(id,false);
    Console.WriteLine("enter to continue");Console.ReadLine();
    Select(id);
      }  static int Create()
      {
    Console.WriteLine("In Create:");
    Configuration cfg = new Configuration();
    cfg.AddXmlFile("userinfo.hbm.xml");
    ISession iS = cfg.BuildSessionFactory().OpenSession();
    ITransaction iT = iS.BeginTransaction();
    UserInfo ui = new UserInfo();
    ui.Name = "August";
    ui.Sex = false;
    ui.Hit = 88;
    ui.Birthday = DateTime.Parse("1983-10-1");
    iS.Save(ui);
    iT.Commit();
    iS.Close();
    return ui.Id;
      }  static void Select(int id)
      {
    Console.WriteLine("In Select:");
    Configuration cfg = new Configuration();
    cfg.AddXmlFile("userinfo.hbm.xml");
    ISession iS = cfg.BuildSessionFactory().OpenSession();
    ITransaction iT = iS.BeginTransaction();
    UserInfo ui = (UserInfo) iS.Load(typeof(UserInfo), id);
    iT.Commit();
    iS.Close();Console.WriteLine("name={0}", ui.Name);
    Console.WriteLine("birthday={0}", ui.Birthday);
      }  static void Update(int id, bool bNull)
      {
    Console.WriteLine("In Update:");
    Configuration cfg = new Configuration();
    cfg.AddXmlFile("userinfo.hbm.xml");
    ISession iS = cfg.BuildSessionFactory().OpenSession();
    ITransaction iT = iS.BeginTransaction();
    UserInfo ui = (UserInfo) iS.Load(typeof(UserInfo), id);
    ui.Birthday = bNull? DateTime.MinValue : DateTime.Now;
    Console.WriteLine("In Update:{0}", ui.Birthday);
    iS.Update(ui);
    iT.Commit();
    iS.Close();
      }
    }
    5.Output:E:\labs\hibernate\TestUser2>TestUser2
    In Create:
    In Select:
    name=August
    birthday=10/1/1983 12:00:00 AM
    In Update:
    In Update:1/1/0001 12:00:00 AM
    enter to continueIn Select:
    name=August
    birthday=1/1/0001 12:00:00 AM
    In Update:
    In Update:12/28/2005 9:51:26 AM
    enter to continueIn Select:
    name=August
    birthday=12/28/2005 9:51:00 AM