0

私はstackoverflowを見回しましたが、答えが本当に見つかりません。たぶん私はそれを見落としています。これは私のコードです。最初の while の後、 while ループに戻り続け、リーダーを閉じた後、 while(reader.read()) に戻ります

   public string VoegHoeveelheidToe(string HandmatigHoeveelheid, string ControleerBarcode)
      {
            string i = null;
            int Beginhoeveelheid;
            int ToeTeVoegenHoeveelheid;
            ToeTeVoegenHoeveelheid = Convert.ToInt32(HandmatigHoeveelheid);
            try
            {
                connectie.Open();
                OleDbDataReader reader = null;
                OleDbCommand command = new OleDbCommand("SELECT hoeveelheid from Voorraad,Product Where Voorraad.Barcode = Product.Barcode AND Voorraad.Barcode = '" + ControleerBarcode + "'", connectie);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Beginhoeveelheid = reader.GetInt32(0);
                    ToeTeVoegenHoeveelheid = Beginhoeveelheid + Convert.ToInt32(HandmatigHoeveelheid);
                    if(ToeTeVoegenHoeveelheid < Beginhoeveelheid)
                    {
                        i = Convert.ToString(3);
                    }
                    else
                    {
                        connectie.Close();
                        try
                        {
                            connectie.Open();
                            OleDbCommand command1 = new OleDbCommand();
                            command1.Connection = connectie;
                            string query = "Update Voorraad set hoeveelheid = " + ToeTeVoegenHoeveelheid + " Where barcode='" + ControleerBarcode + "'";
                            command1.CommandText = query;
                            command1.ExecuteNonQuery();
                            i = Convert.ToString(1);
                            connectie.Close();                                
                        }
                        catch (Exception)
                        {
                            i = Convert.ToString(2);
                        }

                    }
                }

            }
            catch (Exception )
            {
                i = Convert.ToString(2);
            }
            connectie.Close(); return i;
4

1 に答える 1

1

内の接続を閉じていますwhile。これにより、暗黙的にリーダーが閉じられるため、エラーが発生します。基本的に:それをしないでください。と/ 、 、usingを閉じる代わりにステートメントを追加して、ループから抜け出すことを強くお勧めします: 。trycatchfinallybreak

connectie.Close();ループ内にある理由がわかりません。おそらく、「接続上の複数のアクティブなリーダー」の問題のためにそれを追加しました。この場合、最初のリーダーで何をしたいのかに関する情報をバッファリングし、リーダーが終了したときに保留中のコマンドのみを実行する必要があります。または、複数の接続を使用します。

于 2015-01-25T12:24:12.530 に答える