用于更新数据库的按钮不起作用

时间:2016-02-06 13:47:34

标签: c# asp.net visual-studio sql-server-2008 stored-procedures

我有表格的数据库

  • dbo.auctions[id] [productid] [lastbider] [bidvalue] [lastbid]
  • dbo.products[id] [name]
  • dbo.users[id] [name] [password] [bids]

我需要编写一个检查[bids]是否大于0的过程。

如果是,[lastbid]已更新并且[bidvalue]已添加到lastbid = lastbid + bidvalue,则[bids]bids = bids - 1等内容会降低1 Button。我需要将其与Visual Studio中的ALTER PROCEDURE [dbo].[usp_makebid] ( @userid int, @auctionid int ) AS DECLARE @bids int, @lastbid int SELECT @bids = COUNT (*) FROM users WHERE users.id = @userid IF @bids > 0 BEGIN UPDATE auctions SET lastbid = @lastbid + bidvalue WHERE id = @auctionid AND lastbid = @lastbid UPDATE users SET bids = @bids - 1 WHERE bids = @bids END 相关联。

我写了这个程序:

Button

并将其与protected void Button1_Click(object sender, EventArgs e) { using (var con = new SqlConnection(@"data source=JOVAN-PC;database=aukcija_jovan_gajic;integrated security=true")) { con.Open(); var userid = 1; var auctionid = 2; var cmd = new SqlCommand("usp_makebid", con); cmd.Parameters.AddWithValue("@userid", userid); cmd.Parameters.AddWithValue("@auctionid", auctionid); cmd.CommandType = CommandType.StoredProcedure; cmd.ExecuteNonQuery(); con.Close(); } }

相关联
<form:form method="post" action="savePhoto?${_csrf.parameterName}=${_csrf.token}" commandName="document" enctype="multipart/form-data">

但是当我点击按钮时,没有任何反应。

主页看起来像这样,也许可以给你一般的想法:

enter image description here

2 个答案:

答案 0 :(得分:0)

传递userid和auctionid的值,然后更新你的表..

示例:

int userid = 456789;
int auctionid = 12345;

cmd.Parameters.AddWithValue("@userid", SqlDbType.Int).Value = userid;
cmd.Parameters.AddWithValue("@auctionid", SqlDbType.Int).Value = auctionid;

答案 1 :(得分:0)

第一个问题是,您将SqlDbType.Int枚举作为@userid@auctionid参数的值传递,这不是您要做的,而应该提供一个值。< / p>

第二个问题在您的存储过程中。

SELECT bids 
FROM users 
WHERE users.id = @userid

完全没有效果,IF @bids > 0条件总是假的。

猜猜您希望获得users.id = @userid条件的用户数量,那么您应该将上面的选项替换为

SELECT @bids = COUNT(*) 
FROM users 
WHERE users.id = @userid

您最终应该使用以下存储过程:

ALTER PROCEDURE [dbo].[usp_makebid]
(
  @userid INT ,
  @auctionid INT
)
AS 
    DECLARE @bids INT, @lastbid INT

    SELECT @bids = COUNT(*)
    FROM users
    WHERE users.id = @userid

    IF @bids > 0 
    BEGIN
        UPDATE auctions
        SET lastbid = @lastbid + bidvalue
        WHERE id = @auctionid
          AND lastbid = @lastbid

        UPDATE users
        SET bids = @bids - 1
        WHERE bids = @bids
    END

另请注意,cmd.Connection = con;行是多余的,因为Connection已通过SqlCommand构造函数初始化。

试试这个方法:

protected void Button1_Click(object sender, EventArgs e)
{
    using (var con = new SqlConnection(@"data source=JOVAN-PC;database=aukcija_jovan_gajic;integrated security=true"))
    {
        con.Open();
        var userid = 1;
        var auctionid = 2;

        var cmd = new SqlCommand("usp_makebid", con);
        cmd.Parameters.AddWithValue("@userid", userid);
        cmd.Parameters.AddWithValue("@auctionid", auctionid);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.ExecuteNonQuery();
        con.Close();
    }
}