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(); }
}
...全文
224 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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
  • 打赏
  • 举报
回复
没遇到过

61,819

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧