在asp.net中更新数据库 [英] Update database in asp .net

查看:83
本文介绍了在asp.net中更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的项目中,我需要更新已编辑的值。我创建了一个sp,因为它工作得很好。我在数据层,业务层和表示层上获得了所有编辑过的值。但是我的数据库表中没有更新这些值。

这是我的数据层代码:

Hi,
In my project I need to update edited values. I have created an sp for that tha works fine. I got all the edited values on data layer, business layer and presentation layer as well. But the values are not updated in my db table.
Here is my datalayer code:

public object Update(object ObjEditItem)
        {
            try
            {
             BusinessObjects.Common.TransactionDetail objEditTransactionDetail = new BusinessObjects.Common.TransactionDetail();
                string spName = "spUpdateItemHeader";
                Database db = DatabaseFactory.CreateDatabase(ConnectionString);
                DbCommand dbCommand = db.GetStoredProcCommand(spName);
                db.AddInParameter(dbCommand, "@ItemID", DbType.String, ((BusinessObjects.Tables.aItem)ObjEditItem).ItemID);
                db.AddInParameter(dbCommand, "@ItemCode", DbType.String, ((BusinessObjects.Tables.aItem)ObjEditItem).ItemCode);
                db.AddInParameter(dbCommand, "@ItemDescription", DbType.String, ((BusinessObjects.Tables.aItem)ObjEditItem).ItemDescription);

db.AddOutParameter(dbCommand, "@TransactionId", DbType.Int32, 100);
               db.AddOutParameter(dbCommand, "@TransactionNo", DbType.String, 100);
               db.ExecuteScalar(dbCommand);
               if (db.GetParameterValue(dbCommand, "@TransactionId") != DBNull.Value)
               {
                   objEditTransactionDetail.TrnsactionId = Convert.ToInt32(db.GetParameterValue(dbCommand, "TransactionId"));
               }
               if (db.GetParameterValue(dbCommand, "@TransactionNo") != DBNull.Value)
               {
                   objEditTransactionDetail.TrnsactionNo = Convert.ToString(db.GetParameterValue(dbCommand, "TransactionNo"));
               }
               return objEditTransactionDetail;
           }

推荐答案

您正在进行更新,因此它应该是ExecuteNonQuery。 ExecuteScalar是一个只选择语句,它返回一个包含单个列的记录。
You are doing an update, hence it should be ExecuteNonQuery. ExecuteScalar is a select only statement that return a single record that has a single column.


首先查看存储过程:因为你只是传递它值并让它继续工作,你需要查看并确切地知道它在做什么,因为问题可能就在那里。



这可能是个好主意如果它抛出任何异常 - 你没有显示你的 catch 块,所以我们不知道你是在检查异常还是只是丢弃它们。任何错误消息都很可能是相关的。
Start by looking at your stored procedure: since you just pass it values and let it get on with the work, you need to look and see exactly what it is doing with it, since the problem is likely to be there.

It'd probably be a good idea to see if it's throwing any exceptions - you don't show your catch block so we have no idea if you are examining exceptions or just discarding them. Any error message is very likely to be relevant.


这篇关于在asp.net中更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆