将文本框值与数据库值进行比较 [英] comparing textbox values with database values
问题描述
我正在使用以下方法将我的表单文本框值与数据库值id进行比较它们在更新之前是否相同。如果它们不相同,我必须执行某些操作。但是我在表中有很多字段而且我没有想为每个field.its得到非常冗长的代码。我想使用一个非常短的代码使用循环比较它们...使用函数等。
请帮助我..
i am using the following method to compare my forms textbox values with database values id they are same or not before updating.If they are not same i have to perform certain operations.But i have many many fields in table and i dont want to write the following code for every field.its getting very lengthy.I want to compare them using a very short code using looping..using a function etc..
Please help me..
if (!String.Equals(txtMobile.Text.Trim(), objdatarow["MobileNo"].ToString().Trim(), StringComparison.Ordinal))
{
mailstr.Add("MobileNo:-" + txtAddress.Text.Trim());
NewString.Add(txtMobile.Text.Trim());
old.Add(objdatarow["MobileNo"].ToString().Trim());
}
推荐答案
如果您将文本框命名为与数据库字段的名称相同,则可以在表单上执行foreach操作。控制并检查ID甚至使用FindControl。
但是,我不推荐这种方法。单独做这些可能会很痛苦,但我仍然认为你最好单独做这些。如果特定字段不起作用将更容易找到错误,如果您以后必须为字段添加一些特定逻辑,它将更具可读性。
If you named your textboxes the same as the names of your database fields you can do a foreach on Form.Controls and check the ID or even use FindControl.
However, I would not recommend this method. It may be a pain to do them individually but I still think you are better off doing them individually. It will be easier to find bugs if a particular field does not work and if you later have to add some specific logic for a field it would be more readable.
不要在客户端上使用comapre值侧。在服务器端使用存储过程 [ ^ ]( SP)。
Do not comapre values on client side. Do it on server side, using stored procedure[^] (SP).
CREATE PROCEDURE UpdateWhenDiffer
@MobileNo VARCHAR(30),
@MyKey INT
AS
BEGIN
UPDATE MyTable SET MobileNo = @MobileNo
WHERE ID = @MyKey AND MobileNo<>@MobileNo
END
如需了解更多信息,请参阅:
使用存储过程修改数据 [ ^ ]
如何:执行返回单个值的存储过程 [ ^ ]
我建议您使用SP的原因是 SQL注入 [ ^ ]。
如何:防止ASP.NET中的SQL注入 [ ^ ]
停止SQL注入攻击在他们阻止你之前 [ ^ ]
SQL注入及其如何避免 [ ^ ]
For further information, please see:
Modifying Data with Stored Procedures[^]
How to: Execute a Stored Procedure that Returns a Single Value[^]
The reason i suggest you to use SP is SQL Injection[^].
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Injection and how to avoid it[^]
这篇关于将文本框值与数据库值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!