从Foreach循环使用一个变量 [英] Using a variable from a Foreach Loop

查看:133
本文介绍了从Foreach循环使用一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够检查,看是否有相同ID的项目已被放置在数据库中,如果因此然后更新该项目的数量,但由于这样的事实我有
这在foreach循环中,将更新每个项目的数量。

当我在循环我无法使用'ITEMID',因为它不是在上下文之外的命令,反正是有,我可以解决这个问题?

感谢您

 的foreach(UserItem项目编号中(列表< UserItem>)会议[UserSession])
{
    ConclusionPage.InsertCommand =IF EXISTS(SELECT项目编号FROM tblUserItems其中userid ='@的currentUser'AND项ID =@项目编号')UPDATE tblUserItems SET数量=数量+ 1 WHERE(用户ID ='@CurrentUser')和(项目编号='@项目编号');
    ConclusionPage.Insert();
}


解决方案

我想,如果命令( IF EXISTS ... )不是必需的。

试试这个code:

  ConclusionPage.UpdateCommand.Parameters.Add(新System.Data.SqlClient.SqlParameter(@的currentUser,System.Data.SqlDbType.Int,0,System.Data.ParameterDirection。输入,0,0,用户ID,System.Data.DataRowVersion.Current,假,空,,,));
    ConclusionPage.UpdateCommand.Parameters.Add(新System.Data.SqlClient.SqlParameter(@项ID,System.Data.SqlDbType.Int,0,System.Data.ParameterDirection.Input,0,0,项ID系统。 Data.DataRowVersion.Current,假,空,,,));
    ConclusionPage.CommandType = System.Data.CommandType.Text;
    ConclusionPage.UpdateCommand =UPDATE tblUserItems SET数量=数量+ 1 WHERE(用户ID = @CurrentUser)AND(项目编号='@Ite​​mID');    的foreach(UserItem项目编号中(列表< UserItem>)会议[UserSession])
    {
        ConclusionPage.Parameters [0] .value的=的currentUser;
        ConclusionPage.Parameters [1] .value的=项目ID;
        ConclusionPage.ExecuteNonQuery();
    }

I'd like to be able to check to see if an item with the same id has already been placed in the database, if so to then update the quantity for that item, however due to the fact I have this in a foreach loop it will update the quantity for each item.

When I placed the Command outside of the loop I am unable to use 'ItemID' as it's not in context, is there anyway I can get around this?

Thank You

foreach (UserItem ItemID in (List<UserItem>)Session["UserSession"])
{
    ConclusionPage.InsertCommand = "IF EXISTS (SELECT ItemID FROM tblUserItems WHERE UserID='@CurrentUser' AND ItemID='@ItemID')  UPDATE tblUserItems SET Quantity = Quantity+1 WHERE (UserID = '@CurrentUser') AND (ItemID = '@ItemID')";
    ConclusionPage.Insert();                 
}

解决方案

I think if command (IF EXISTS ... ) not required.

Try this code:

    ConclusionPage.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CurrentUser", System.Data.SqlDbType.Int, 0, System.Data.ParameterDirection.Input, 0, 0, "UserID", System.Data.DataRowVersion.Current, false, null, "", "", ""));
    ConclusionPage.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ItemID", System.Data.SqlDbType.Int, 0, System.Data.ParameterDirection.Input, 0, 0, "ItemID", System.Data.DataRowVersion.Current, false, null, "", "", ""));
    ConclusionPage.CommandType = System.Data.CommandType.Text;
    ConclusionPage.UpdateCommand = "UPDATE tblUserItems SET Quantity = Quantity+1 WHERE (UserID = @CurrentUser) AND (ItemID = '@ItemID')";

    foreach (UserItem ItemID in (List<UserItem>)Session["UserSession"])
    {
        ConclusionPage.Parameters[0].Value = CurrentUser;
        ConclusionPage.Parameters[1].Value = ItemID;
        ConclusionPage.ExecuteNonQuery();
    }

这篇关于从Foreach循环使用一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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