2013-06-04 144 views
1

我的用户表中的每一列的值更改事务表我有一个数据插入记录到在SQL

User_Name 
User_Address 
User_Gender 

等用户表..

现在我的交易表中包含的字段,如:

Trans_Id 
Trans_User_Field 
Trans_Previuos_Data 
Trans_Add_Date 

现在在我的ASP.net应用程序,当用户更新他们的地址或名称,或者有比较与用户表并插入一条记录每个更新的字段/列到页面上的任何其他领域交易表与以前的数据。

这里Trans_User_field给你哪个字段更新(User_Name的,User_Address,User_Gender)

请告诉我什么是做到这一点的最好办法。在SQL端或应用端进行。

THanks

+0

难道你只是从用户表中获取当前值,在事务表中创建新记录,将更改的值,日期和用户已更改的字段?我想我不明白你遇到什么麻烦。 – mason

+0

我的网页上有200个字段,我不知道如何识别哪个字段用户已经更改。 – user1882705

+0

您如何向用户呈现数据?它是一个GridView? – mason

回答

0

假设您正在使用ASP.NET Web窗体。

在.aspx页面

TextBox1: <asp:TextBox runat="server" id="TextBox1" /><br /> 
TextBox2: <asp:TextBox runat="server" id="TextBox2" /><br /> 
<asp:Button runat="server" id="Button1" OnClick="Button1_Click" Text="Submit" /> 

在你.aspx.cs页

protected void Button1_Click(object sender, EventArgs e) 
{ 
     string original_text=GetOriginalTextOfTextBox1(); 
     if(TextBox1.Text==original_text) 
     { 
      //the text didn't change 
     } 
     else 
     { 
      //the text changed. need to update the transaction table and the user table. 
     } 
     string originaltext2=GetOriginalTextOfTextBox2(); 
     if(TextBox2.Text==originaltext2) 
     { 
      //the text didn't change 
     } 
     else 
     { 
      //the text changed. need to update the transaction table and the user table. 
     } 

} 
protected string GetOriginalTextOfTextBox1() 
{ 
    //code here that gets the original value of TextBox1 from the User table. 
} 
protected string GetOriginalTextOfTextBox2() 
{ 
     //code here that gets the original value of TextBox2 from the User table. 
} 

}

你可能会想这一切使用集合(列表)相结合,一旦你有了这个概念,强类型的对象。这将最大限度地减少对数据库的调用次数并简化代码。

- 编辑 - 如果要使用Transcation表中的单个记录存储单个更新的所有历史记录,则需要修改Transaction表以一次支持所有字段。请注意,这可能不是空间有效的,这取决于您是希望用户更新每个事务的许多字段还是仅更新一个字段。

Trans_Id 
User_Name 
User_Gender 
User_Address 
Trans_Add_Date 
+0

在这个C#代码我将如何分隔每个字段,以知道此字段已更新到事务表。因为我只将该字段/列插入到事务处理表中 – user1882705

+0

用多个记录更新事务处理表,每个字段更新一个。 – mason

+0

我不想为用户页面中的一个字段更新插入多条记录 – user1882705

1

虽然我可能会因此而受到谴责,因为人们强烈憎恨触发器,所以我会在此建议一个。你可以建立一个这样的:

CREATE TRIGGER update_user ON table FOR UPDATE 
AS 

DECLARE @update_mask AS INT 
SELECT @update_mask = COLUMNS_UPDATED() 

IF (@update_mask & 1 = 1) -- this means the first column was modified 
IF (@update_mask & 2 = 2) -- this means the second column was modified 
IF (@update_mask & 4 = 4) -- this means the third column was modified 
IF (@update_mask & 8 = 8) -- this means the fourth column was modified 

,我认为你的想法。从那里你可以从updated行和INSERT获取更新的值到另一个表中。请参阅使用COLUMNS_UPDATED方法为您提供一些真正的灵活性。如果设置了的列,则可以通过将它们的位值添加在一起并仅查找它来轻松确定。所以我们可以说,我想知道,如果地址和性别均被改变 - 因为不管是什么原因 - 我可以这样做:

IF (@update_mask & 6 = 6) -- both the second the third fields were modified 
+0

我认为这不会在我的情况下工作...我的桌子上有近200列... – user1882705

1

如何尝试了另一种途径。创建一个Trans_User表将会在用户表和Trans_Date中的所有字段。然后在用户表上创建插入/更新/删除触发器,以使用先前的数据填充Trans_User表。看看这个questionthis代码项目文章。