每次更新时,当前日期时间都需要存储在DB中 [英] current datetime need to be stored in DB each time while updating

查看:65
本文介绍了每次更新时,当前日期时间都需要存储在DB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用datetimepicker控件。如果我按UPDATE按钮,每次按下系统日期和时间都需要存储在数据库中,(如果我按3次然后3次应该存储)它不应该覆盖... \\它可能帮我解决

i am using datetimepicker control. if i press UPDATE button, each time while pressing system date and time need to stored in database,(if i press 3 times then 3 times it should store) it should not overwrite...\\is it possible help me out

推荐答案

在数据库中插入三行。

你不需要更新专栏。

Insert three rows into the database.
You don''t need to update the column.
Table1
Row1 -> A,B,C,T1
Row2 -> A,B,C,T2
Row3 -> A,D,E,T3





另一个更简洁的解决方案可能是在一个列中插入一个id,该列指向另一个存储a的表每个日期时间更新的条目数。



Another neater solution might be to insert an id into a column which points to another table that stores a number of entries for each datetime update.

Table1
Row1 -> A,B,C,id1
Row2 -> A,B,C.id1
Row3 -> A,D,E,id2

Table2
Row1 -> id1, T1
Row2 -> id1, T2
Row3 -> id3, T3


当然有可能,但你的问题需要更多的澄清,比如表结构。如何准确存储?

但是让我们尝试回答它...

你可以通过多种方式实现这一目标。以下是其中两个:

1)您创建一个包含外键的其他表,以便将时间戳和时间戳链接起来。在每个更新中,您不仅更新了您想要的内容,还在此表中插入一行。

2)创建一个包含外键的其他表,以便将时间戳和时间戳链接起来。并且你在主表上创建一个更新(也可能是插入)触发器,在第二个表中插入一条记录。



确保有好处时间戳,你应该让服务器给它一个值。请参阅: http://msdn.microsoft.com/en-us/library/ms188383.aspx [ ^ ]



[更新]

见以下示例:

Of course it is possible, but your question needs more clarification, like table structure. How to store exactly?
But let''s try answering it...
You can achieve this in many ways. Here are two of them:
1) You make an other table that contains a foreign key to whatever you want to link the timestamp and the timestamp. On every Update you not only update what you want, but also insert a row in this table.
2) make an other table that contains a foreign key to whatever you want to link the timestamp and the timestamp. And you create an on update (and probably on insert too) trigger on the primary table that inserts a record in this second table.

To be sure to have the good timestamp, you should let the server give it a value. See: http://msdn.microsoft.com/en-us/library/ms188383.aspx[^]

[Update]
See following sample:
CREATE TABLE [dbo].[balance_history](
    [ref_id] [int] NOT NULL,
    [balance] [money] NOT NULL,
    [total] [money] NOT NULL,
    [timestamp] [datetime] NOT NULL
) ON [PRIMARY]







CREATE TABLE [dbo].[balances](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [balance] [money] NOT NULL,
    [total] [money] NOT NULL,
 CONSTRAINT [PK_balances] PRIMARY KEY CLUSTERED ( [id] ASC )
) ON [PRIMARY]







CREATE TRIGGER [dbo].[TR_STORE_]
   ON  [dbo].[balances] AFTER UPDATE
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO BALANCE_HISTORY(ref_id, balance, total, [timestamp])
    SELECT id, balance, total, GETDATE()
    FROM DELETED;

END


这篇关于每次更新时,当前日期时间都需要存储在DB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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