保存 sql 货币数据类型的变量不会更新 (C#)

本文关键字:更新 变量 sql 货币 数据类型 保存 | 更新日期: 2023-09-27 18:37:19

我的sql服务器数据库中有一个列类型的钱,它是moneyAcumulated。然后,对于一个command对象,我执行一个存储过程,该过程 - 除其他外 - 为列带来moneyAcumulated的值。然后在 while 循环中,我将存储过程返回的每一列的所有值存储在变量中。

SqlCommand command = new SqlCommand("getTickets", Connection.getConnection());
            command.CommandType = CommandType.StoredProcedure;
            SqlDataReader reader = command.ExecuteReader();
            string bet = "";
            decimal moneyAcumulated = 0.0M;
            string id= "";
            string name = "";
            int cod = -1;
            decimal prize = 0.0M;
            while (reader.Read())
            {
                bet = reader.GetString(0);
                moneyAcumulated = reader.GetDecimal(1); // This variable doesn't chage its value
                name = reader.GetString(2);
                id = reader.GetString(3);
                cod = reader.GetInt32(4);
            }

第一次循环迭代每个变量时采用正确的值(包括带来readermoneyAcumulated,但第二次moneyAcumulated不会改变其值,而其他变量会改变,为什么?

保存 sql 货币数据类型的变量不会更新 (C#)

问题是 SqlDataReader 将command.ExecuteReader返回的所有数据加载到内存中,那么如果数据库中的数据发生变化,它们在内存中就不会改变,我认为每次程序执行 reader.Read() 方法时它们都会改变,所以这就是 is 不起作用的原因。由于数据库中的moneyAcumulated字段最初对于所有让我更加困惑的元组都是相同的,因为其他元组正在更改,而moneyAcumulated则没有。