开发者

Update sql table value after clickbtn

I have table with items:

id | title | ordersTillNow

I want to write c# code on the button click event so that after the customer clicks on purchase button the column ordersTillNow will increase by one (++)...

开发者_开发知识库

Can this be done with Linq-to-SQL ? Or with SqlCommand?

Anyway ...how can I do that on c# (code behind, with Linq-to-SQL or SqlCommand) ?


Assuming you're using Linq-to-SQL, you will have a data context for your database, and an object that represents your table.

When the customers click, you'd write code something like this:

using (YourDataContext ctx = new YourDataContext())
{
    Customer myCust = from c in ctx.Customers
                      where c.CustomerId == ID
                     select c;

    myCust.ordersTillNow++;
    ctx.SubmitChanges()
}

Using a SqlCommand, you have lots of options to do this - a stored procedure, inline SQL - whatever.

You'll write code something like this:

string updateStmt = "UPDATE dbo.YourTable SET ordersTillNow = ordersTillNow + 1 " +
                    "WHERE CustomerID = @CustomerID";

using(SqlConnection _conn = new SqlConnection("your-connection-string-here"))
using(SqlCommand _cmd = new SqlCommand(_conn, updateStmt))
{
   _cmd.Parameters.Add("@CustomerID", SqlDbType.Int).Value = yourCustomerID;
   _conn.Open();
   _cmd.ExecuteNonQuery();
   _conn.Close();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜