1

SQLサーバーデータベースに列タイプmoneyがあります。これは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);
            }

最初にwhileループが繰り返されると、各変数は正しい値を取ります(これには、が含まmoneyAcumulatedれますがreader、2回目moneyAcumulatedはその値が変更されず、他の変数は変更されます。なぜですか?

4

1 に答える 1

1

問題は、SqlDataReader がメモリ内で返されたすべてのデータをロードしcommand.ExecuteReader、データベース内でデータが変更された場合、メモリ内で変更されないことです。プログラムがメソッドを実行するたびにデータが変更されると思っていたreader.Read()ので、それが理由でした。動作していません。そしてmoneyAcumulated、データベースのフィールドは最初はすべてのタプルで同じであるため、他のタプルは変化してmoneyAcumulatedいないのに変化していたため、私をさらに混乱させました。

于 2012-06-16T05:51:59.040 に答える