我如何在更新之前保留记录在数据库中的变量值? [英] how can i keep a value which was recorded in database in variable before i updated?
问题描述
在更新变量之前,如何在数据库中保留记录在数据库中的值?我希望将旧值用于具有新值的操作,
例如:
NewValue-OldValue = VariableValue
230-200 = 30,
顺便说一句,在我更新它之前,OldValue是NewValue.
如果可以的话,你能给我一个想法吗?
how can keep a value which it was recorded in database in variable before i updated it? i want the old value for an operation with new value,
example:
NewValue - OldValue = VariableValue
230 - 200 = 30,
by the way the OldValue is NewValue before i updated it.
can u give me an idea about it, if it''s possible.
private void ChargerKiloVoitures()
{
try {
_bd.Command.CommandText = "SELECT Kilometrage FROM Voitures " +
"WHERE NumeroMatricule = \"" + CBNumMatricule.Text + "\"";
Debug.WriteLine( Environment.NewLine );
Debug.WriteLine( _bd.Command.CommandText );
_bd.Reader = _bd.Command.ExecuteReader();
if ( _bd.Reader.Read());
this.analogCounter2.Number = int.Parse(_bd.Reader["Kilometrage"].ToString());
this.analogCounter1.Number = this.analogCounter2.Number + VidangeConstant;
//i have problem here exactly
int OldKilometrage = int.Parse(_bd.Reader["Kilometrage"].ToString());
int NewKilometrage = int.Parse(_bd.Reader["Kilometrage"].ToString());
KiloMetrag.Value = NewKilometrage-OldKilometrage;
}//if
_bd.Reader.Close();
}//try
catch (Exception ex) {
Debug.WriteLine( Environment.NewLine );
Debug.WriteLine( ex.Message );
MessageBox.Show("Une erreur a été détectée : " + Environment.NewLine + ex.Message, "Erreur");
}//catch
}
推荐答案
您要保留"什么?您正在显示计算,就好像您试图证明一些数学运算一样.编程语言不能那样工作.
您将拥有一个接受更改的函数,该函数带有如下签名:
What do you want to "keep"? You are showing calculations as though you''re trying to prove some math operation. Programming languages don''t work like that.
You will have a function that accepts the changes, something with a signature like:
private void UpdateDatabase(int myItemID, int newValue)
{
// this is where you should be updating things
}
此时,您有了newValue
,并且在更新数据库之前,您只需从数据库中获取旧值的副本即可.使用查询获取值.
从那里,您可以执行所需的任何数学运算,登录或进行任何曲调操作.
干杯.
At this point, you have the newValue
, and before you update the database, you can just fetch a copy of the old value from the database. Use a query to get the value.
From there, you can do whatever math you want to, or log, or whatever turns your crank.
Cheers.
将其另存为单独的变量.
顺便说一句,为什么您要从代码中表示的方式向后制作示例?这无非是使人们感到困惑.
Save it as a separate variable.
BTW, why are you making your examples backwards from the way it''s represented in the code? That does nothing more than confuse people.
这篇关于我如何在更新之前保留记录在数据库中的变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!