• 主页
  • ASP
  • .NET Framework
  • Web Services
  • VB
  • VC
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • LINQ

NHibernate , SqlHelper 使用中的大问题

Hunts 新蛋信息技术(中国)有限公司 开发组长/高级工程师/技术专家  2008-07-11 05:20:43
系统中的存储层,使用到了NHibernate和 SqlHelper

NHibernate 有两种使用方法
1.O/R Mapping

public Article GetArticleById(int id)
{
Article a = new Article();
session = NHibernateHelper.GetCurrentSession();
ITransaction tx = session.BeginTransaction();
try
{
a = session.Get<Article>(id);
tx.Commit();
return a;
}
catch (HibernateException ex)
{
if (null != tx) tx.Rollback();
throw new ApplicationException(ex.Message);
}
finally { NHibernateHelper.CloseSession(); }
}

2. 调用存储过程

public IList<ArticleInfo> GetArticles(int CatId)
{
IList<ArticleInfo> list = new List<ArticleInfo>();
// 使用存储过程
session = NHibernateHelper.GetCurrentSession();
IDbCommand cmd = NHibernateHelper.CreateCommand(session, CommandType.StoredProcedure, "Article_GetArticles");
IDbDataParameter param = cmd.CreateParameter();
param.ParameterName = "CatId";
param.Value = CatId;
cmd.Parameters.Add(param);
try
{
IDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
list.Add(new ArticleInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetDateTime(4), rdr.GetInt32(5), rdr.GetInt32(6)));
}
return list;
}
catch (HibernateException ex)
{
throw new ApplicationException(ex.Message);
}
finally { NHibernateHelper.CloseSession(); }


但是时不时出现下面的错误
The server failed to resume the transaction. Desc:33000000ad.

刷新一下就变成

Specified cast is not valid.

据说是“脏数据”的问题,请问如何解决呢?

另外有时还这么用
3.

public int InsertArticle(Article obj)
{
int artcileid = 0;
session = NHibernateHelper.GetCurrentSession();
ITransaction tx = session.BeginTransaction();
try
{
artcileid = (int)session.Save(obj);
tx.Commit();

// 将Tags加入TagMap表
SqlHelper.ExecuteNonQuery(SqlHelper.KyZhiSqlConn, CommandType.StoredProcedure, "Tag_SaveTags",
new SqlParameter("@EntityId", obj.ID),
new SqlParameter("@EntityType", TagEntityType.article),
new SqlParameter("@Tags", obj.Tags));

return artcileid;
}
catch (HibernateException ex)
{
if (null != tx) tx.Rollback();
throw new ApplicationException(ex.Message);
}
finally { NHibernateHelper.CloseSession(); }
}
...全文
164 点赞 收藏 8
写回复
8 条回复
Hunts 2008年07月12日
再一种情况

Server Error in '/' Application.
--------------------------------------------------------------------------------

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
KyZhi.DAL.MessageDAL.GetUnreadedSum(Guid userId) +180
KyZhi.BLL.MessageBL.GetUnreadedSum(Guid userId) +40
MasterPage.Page_Load(Object sender, EventArgs e) +96
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Control.LoadRecursive() +131
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

回复 点赞
Hunts 2008年07月12日
另一种情况

Server Error in '/' Application.
--------------------------------------------------------------------------------

Settings
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Settings

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[IndexOutOfRangeException: Settings]
System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName) +1433771
System.Data.SqlClient.SqlDataReader.GetOrdinal(String name) +102
System.Data.SqlClient.SqlDataReader.get_Item(String name) +12
KyZhi.DAL.SiteSetting.PopulateSiteSettingsFromIDataReader(IDataReader dr) +76
KyZhi.DAL.SiteSetting.GetSiteSettings() +162
KyZhi.BLL.SiteSettingDataProxy.GetSiteSettings() +58
KyZhi.Web.Setting.get_SiteSettings() +5
MasterPage.Page_Load(Object sender, EventArgs e) +431
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Control.LoadRecursive() +131
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436
回复 点赞
Hunts 2008年07月12日
一种情况

Server Error in '/' Application.
--------------------------------------------------------------------------------

The server failed to resume the transaction. Desc:3500000070.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: The server failed to resume the transaction. Desc:3500000070.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[SqlException (0x80131904): The server failed to resume the transaction. Desc:3500000070.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +925466
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +800118
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +186
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1932
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +31
System.Data.SqlClient.SqlDataReader.get_MetaData() +62
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1005
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteScalar() +137
KyZhi.DAL.MessageDAL.GetUnreadedSum(Guid userId) +158
KyZhi.BLL.MessageBL.GetUnreadedSum(Guid userId) +40
MasterPage.Page_Load(Object sender, EventArgs e) +96
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Control.LoadRecursive() +131
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436
回复 点赞
Jinglecat 2008年07月12日
请准确告诉我们错误信息,特别是堆栈信息,比如哪行抛出的异常
回复 点赞
Hunts 2008年07月11日
[Quote=引用 3 楼 sheng9hhd 的回复:]
是不是类型不对,或有null?
[/Quote]
不但是上面这几段代码,凡是这样写的地方都有出错的可能。
回复 点赞
sheng9hhd 2008年07月11日
是不是类型不对,或有null?
回复 点赞
Hunts 2008年07月11日
ding
回复 点赞
踏雪听雨 2008年07月11日
没遇到过
回复 点赞
发动态
发帖子
.NET技术社区
创建于2007-09-28

4.9w+

社区成员

66.8w+

社区内容

.NET技术交流专区
社区公告
暂无公告