[已关闭问题]
关闭于 2008-08-21 16:23
持久化的C#类:<br>------------------------------------------------------------------------------------------------------------<br>namespace C.Student<br>{<br> #region UserInfo<br><br> /// <summary><br> /// UserInfo object for NHibernate mapped table 'UserInfo'.<br> /// </summary><br> public class UserInfo : System.IComparable<br> {<br> #region Member Variables<br> <br> protected int _id;<br> protected string _userName;<br> protected string _userPwd;<br> protected string _userType;<br> protected string _userTitle;<br> protected int _sum;<br> protected static String _sortExpression = "Id";<br> protected static SortDirection _sortDirection = SortDirection.Ascending;<br><br> #endregion<br><br> #region Constructors<br><br> public UserInfo() { }<br><br> public UserInfo( string userName, string userPwd, string userType, string userTitle, int sum )<br> {<br> this._userName = userName;<br> this._userPwd = userPwd;<br> this._userType = userType;<br> this._userTitle = userTitle;<br> this._sum = sum;<br> }<br><br> #endregion<br><br> #region Public Properties<br><br> public int Id<br> {<br> get {return _id;}<br> set {_id = value;}<br> }<br><br> public string UserName<br> {<br> get { return _userName; }<br> set<br> {<br> if ( value != null && value.Length > 50)<br> throw new ArgumentOutOfRangeException("Invalid value for UserName", value, value.ToString());<br> _userName = value;<br> }<br> }<br><br> public string UserPwd<br> {<br> get { return _userPwd; }<br> set<br> {<br> if ( value != null && value.Length > 50)<br> throw new ArgumentOutOfRangeException("Invalid value for UserPwd", value, value.ToString());<br> _userPwd = value;<br> }<br> }<br><br> public string UserType<br> {<br> get { return _userType; }<br> set<br> {<br> if ( value != null && value.Length > 50)<br> throw new ArgumentOutOfRangeException("Invalid value for UserType", value, value.ToString());<br> _userType = value;<br> }<br> }<br><br> public string UserTitle<br> {<br> get { return _userTitle; }<br> set<br> {<br> if ( value != null && value.Length > 50)<br> throw new ArgumentOutOfRangeException("Invalid value for UserTitle", value, value.ToString());<br> _userTitle = value;<br> }<br> }<br><br> public int Sum<br> {<br> get { return _sum; }<br> set { _sum = value; }<br> }<br><br> public static String SortExpression<br> {<br> get { return _sortExpression; }<br> set { _sortExpression = value; }<br> }<br><br> public static SortDirection SortDirection<br> {<br> get { return _sortDirection; }<br> set { _sortDirection = value; }<br> }<br> #endregion<br> <br> #region IComparable Methods<br> public int CompareTo(object obj)<br> {<br> if (!(obj is UserInfo))<br> throw new InvalidCastException("This object is not of type UserInfo");<br> <br> int relativeValue;<br> switch (SortExpression)<br> {<br> case "Id":<br> relativeValue = this.Id.CompareTo(((UserInfo)obj).Id);<br> break;<br> case "UserName":<br> relativeValue = (this.UserName != null) ? this.UserName.CompareTo(((UserInfo)obj).UserName) : -1;<br> break;<br> case "UserPwd":<br> relativeValue = (this.UserPwd != null) ? this.UserPwd.CompareTo(((UserInfo)obj).UserPwd) : -1;<br> break;<br> case "UserType":<br> relativeValue = (this.UserType != null) ? this.UserType.CompareTo(((UserInfo)obj).UserType) : -1;<br> break;<br> case "UserTitle":<br> relativeValue = (this.UserTitle != null) ? this.UserTitle.CompareTo(((UserInfo)obj).UserTitle) : -1;<br> break;<br> case "Sum":<br> relativeValue = (this.Sum != null) ? this.Sum.CompareTo(((UserInfo)obj).Sum) : -1;<br> break;<br> default:<br> goto case "Id";<br> }<br> if (UserInfo.SortDirection == SortDirection.Ascending)<br> relativeValue *= -1;<br> return relativeValue;<br> }<br> #endregion<br> }<br><br> #endregion<br>}<br>------------------------------------------------------------------------------------------------------------<br>NHibernate映射文件:<br><?xml version="1.0" encoding="utf-8" ?><br><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy ="false"><br> <class name="C.Student.UserInfo, C.Student" table="UserInfo"><br> <id name="Id" type="Int32" unsaved-value="null"><br> <column name="UserID" length="4" sql-type="int" not-null="true" unique="true" index="PK_UserInfo"/><br> <generator class="native" /><br> </id><br> <property name="UserName" type="String"><br> <column name="UserName" length="50" sql-type="nvarchar" not-null="false"/><br> </property><br> <property name="UserPwd" type="String"><br> <column name="UserPwd" length="50" sql-type="nvarchar" not-null="false"/><br> </property><br> <property name="UserType" type="String"><br> <column name="UserType" length="50" sql-type="nvarchar" not-null="false"/><br> </property><br> <property name="UserTitle" type="String"><br> <column name="UserTitle" length="50" sql-type="nvarchar" not-null="false"/><br> </property><br> <property name="Sum" type="Int32"><br> <column name="Sum" length="4" sql-type="int" not-null="false"/><br> </property><br> </class><br></hibernate-mapping><br>------------------------------------------------------------------------------------------------------<br>App.config:<br><?xml version="1.0" encoding="utf-8" ?><br><configuration><br> <configSections><br> <section name="hibernate-configuration"<br> type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /><br> </configSections><br> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"><br> <session-factory><br> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property><br> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property><br> <property name="connection.connection_string"><br> Server=(local);initial catalog=Chjw8016;Integrated Security=SSPI<br> </property><br> <property name="show_sql">true</property><br> <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property><br> </session-factory><br> </hibernate-configuration><br></configuration><br>--------------------------------------------------------------------------------------------------------<br>测试代码:<br>using System;<br>using System.Collections.Generic;<br>using System.Text;<br>using NHibernate;<br>using NHibernate.Cfg;<br>using C.Student;<br>namespace ConsoleApp<br>{<br> class Program<br> {<br> static void Main(string[] args)<br> {<br> Configuration conf = new Configuration().AddAssembly("C.Student") ;<br> ISessionFactory factory = conf.BuildSessionFactory();<br> ISession session = factory.OpenSession();<br><br><br> UserInfo user = new UserInfo();<br> user.UserName = "Chjw8016";<br> ITransaction trans = session.BeginTransaction();<br> try<br> {<br> session.Save(user);<br> trans.Commit();<br> session.Close();<br> Console.WriteLine("Insert Success!");<br> Console.ReadLine();<br> }<br> catch (Exception ex)<br> {<br> trans.Rollback();<br> session.Close();<br> Console.WriteLine(ex.Message);<br> Console.ReadLine();<br> }<br><br> }<br> }<br>}<br>-------------------------------------------------------------------------------------------------------------------------------<br>问题:<br><img src="http://www.cnblogs.com/images/cnblogs_com/chjw8016/UseInCode/11111.PNG" mce_src="http://www.cnblogs.com/images/cnblogs_com/chjw8016/UseInCode/11111.PNG" height="185" width="670"><br>不知道是什么会事,老是插入不成功!!不知道问题出在那里????<br>
问题补充:
持久化类和映射文件,都是CodeSimth生成的。
还有我用的是nhibernate2.0