自动将值插入数据库 [英] Inserting the value in database automatically

查看:80
本文介绍了自动将值插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为集合借阅期限的表格.


我有一个文本框,一个更新按钮..

系统要求:
系统应能够将这些持续时间添加到返回书中的日期.例如.发布日期为6月1日,则退还日期为发布日期+ 14天,即6月15日为退还日期.

我试过像这样的::


private void buttonUpdate_Click(对象发送者,EventArgs e)
{
SqlConnection connect =新的SqlConnection(str);
connect.Open();
{
字符串newdate = textBox_Borrow.Text;
字符串sqlquery ="UPDATE Burrow_Duration SET Days = @ newdate";
SqlCommand cmd =新的SqlCommand(sqlquery,connect);
cmd.Parameters.AddWithValue("@ newdate",textBox_Borrow.Text);
cmd.ExecuteNonQuery();
this.Close();


当执行上面的代码时,如果挖洞持续时间是5,那么当我在文本框中键入10时,数据库将得到更新,即10出现在数据库中.因为我有插入更新查询.
但是我想如果5 + 14 = 19(14)应该被自动插入...
10 + 14 = 24(14)应该自动插入!!!

有人可以帮我吗!
首先,您只需要使用where子句设置有关书籍的日期即可.其次,您需要计算所需的日期.最好的方法是在存储的proc中传递一个日期,然后proc从那里的值加上传递的值来计算所需的值.否则,您需要向数据库询问当前值,并用代码对其进行计算.


使用触发器...
或使用编码,阅读编号.首先从数据库中删除数据,然后进行更改,然后更新数据库中更改的值.
我认为,触发器是更好的解决方案.


i have one form named set borrow duration.


where i have one textbox,one update button..

Requirement of the system:
The system shall be able to add these durations to return book date. E.g. 1st june is issued date then the return date SHALL be issue date+14 days i.e. 15 June SHALL be returned date of book.

i have tried likes this::


private void buttonUpdate_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection(str);
connect.Open();
{
string newdate = textBox_Borrow.Text;
string sqlquery = "UPDATE Burrow_Duration SET Days=@newdate";
SqlCommand cmd = new SqlCommand(sqlquery, connect);
cmd.Parameters.AddWithValue("@newdate", textBox_Borrow.Text);
cmd.ExecuteNonQuery();
this.Close();


while executing the above code if burrow duration days is 5 then when i type 10 in textbox, database gets update ie 10 comes in database..because i have insert update query.
but i want like if 5+14=19 (14)should be automatically be inserted...
10+14=24 (14) should be automatically be Inserted!!!

can any body help me out!!!<strike></strike>

解决方案

You need to do a few things.

First of all, you need to set the date for only the book in question, with a where clause. Second, you need to calculate the date you want. The best way to do this, is in a stored proc, where you pass in a date, and the proc calculates the value you want from the value there, plus the value you passed in. Otherwise, you need to ask the DB for the current value, and calculate it in code.


Use triggers...
Or use coding, read the the no. of days from the database first and then make changes to and then update the changed value in database.
I think, trigger is better solution.


这篇关于自动将值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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