最佳实践 - 处理多个字段,用户角色和一个存储过程

时间:2022-02-25 16:38:19

I have multiple fields both asp:DropDownList's and asp:TextBox's. I also have a number of user roles that change the Visible property of certain controls so the user cannot edit them. All of this data is saved with a stored procedure call on PostBack. The problem is when I send in the parameters and the control was not on the page obviously there wasn't a value for it, so in the stored procedure I have the parameters initialized to null. However, then the previous value that was in the database that I didn't want changed is overwritten with null.

我有多个字段:asp:DropDownList和asp:TextBox。我还有许多用户角色可以更改某些控件的Visible属性,因此用户无法编辑它们。所有这些数据都通过PostBack上的存储过程调用保存。问题是当我发送参数并且控件不在页面上时显然没有它的值,所以在存储过程中我将参数初始化为null。但是,那么我想要更改的数据库中的先前值将被null覆盖。

This seems to be a pretty common problem, but I didn't have a good way of explaining it. So my question is, how should I go about keeping some fields from being on the page but also keeping the values in the database all with one stored procedure?

这似乎是一个非常普遍的问题,但我没有很好的解释方法。所以我的问题是,我应该如何保持一些字段不在页面上,而且还要用一个存储过程保存数据库中的值?

4 个解决方案

#1


Apply the same logic when chosing what data to update as the logic you're actually using when chosing what data (and its associated UI) to render.

在选择要更新的数据时,应用相同的逻辑作为您在选择要渲染的数据(及其关联的UI)时实际使用的逻辑。

#2


I think the problem is you want to do the update of all fields in a single SQL update, regardless of their value.

我认为问题是您希望在单个SQL更新中更新所有字段,而不管它们的值如何。

I think you should do some sanity check of your input before your update, even if that implies doing individual updates for certain parameters.

我认为您应该在更新之前对输入进行一些完整性检查,即使这意味着要对某些参数进行单独更新。

#3


Without an example, it is a little difficult to know your exact circumstances, but here is a fictitious statement that will hopefully give you some ideas. It is using t-sql (MS SQL Server) since you did not mention a specific version of SQL:

没有一个例子,知道你的具体情况有点困难,但这是一个虚构的陈述,希望能给你一些想法。它使用的是t-sql(MS SQL Server),因为您没有提到特定版本的SQL:

UPDATE SomeImaginaryTable
SET FakeMoneyColumn = COALESCE(@FakeMoneyValue, FakeMoneyColumn)
WHERE FakeRowID = @FakeRowID

This basically updates a column to the parameter value, unless the parameter is null, in which case it uses the columns existing value.

这基本上将列更新为参数值,除非参数为null,在这种情况下它使用列现有值。

#4


Generally to overcome this in my update function

一般在我的更新功能中克服这一点

  1. I would load the current values for the user
  2. 我会为用户加载当前值

  3. Replacing any loaded values with the newly changed values from the form
  4. 使用表单中新更改的值替换任何已加载的值

  5. Update in db.
  6. db中的更新。

This way I have all the current plus everything that has been changed will get changed.

这样我就拥有了所有当前的内容以及所有已更改的内容都将被更改。

This logic will also work for an add form because all the fields would be null then get replaced with a new value before being sent to the db. You would of course just have to check whether to do an insert or update.

此逻辑也适用于添加表单,因为所有字段都将为null,然后在发送到db之前将其替换为新值。您当然只需要检查是否进行插入或更新。

#1


Apply the same logic when chosing what data to update as the logic you're actually using when chosing what data (and its associated UI) to render.

在选择要更新的数据时,应用相同的逻辑作为您在选择要渲染的数据(及其关联的UI)时实际使用的逻辑。

#2


I think the problem is you want to do the update of all fields in a single SQL update, regardless of their value.

我认为问题是您希望在单个SQL更新中更新所有字段,而不管它们的值如何。

I think you should do some sanity check of your input before your update, even if that implies doing individual updates for certain parameters.

我认为您应该在更新之前对输入进行一些完整性检查,即使这意味着要对某些参数进行单独更新。

#3


Without an example, it is a little difficult to know your exact circumstances, but here is a fictitious statement that will hopefully give you some ideas. It is using t-sql (MS SQL Server) since you did not mention a specific version of SQL:

没有一个例子,知道你的具体情况有点困难,但这是一个虚构的陈述,希望能给你一些想法。它使用的是t-sql(MS SQL Server),因为您没有提到特定版本的SQL:

UPDATE SomeImaginaryTable
SET FakeMoneyColumn = COALESCE(@FakeMoneyValue, FakeMoneyColumn)
WHERE FakeRowID = @FakeRowID

This basically updates a column to the parameter value, unless the parameter is null, in which case it uses the columns existing value.

这基本上将列更新为参数值,除非参数为null,在这种情况下它使用列现有值。

#4


Generally to overcome this in my update function

一般在我的更新功能中克服这一点

  1. I would load the current values for the user
  2. 我会为用户加载当前值

  3. Replacing any loaded values with the newly changed values from the form
  4. 使用表单中新更改的值替换任何已加载的值

  5. Update in db.
  6. db中的更新。

This way I have all the current plus everything that has been changed will get changed.

这样我就拥有了所有当前的内容以及所有已更改的内容都将被更改。

This logic will also work for an add form because all the fields would be null then get replaced with a new value before being sent to the db. You would of course just have to check whether to do an insert or update.

此逻辑也适用于添加表单,因为所有字段都将为null,然后在发送到db之前将其替换为新值。您当然只需要检查是否进行插入或更新。