Pergunta

Using a while loop I am iterating through string values stored in a database -

            while (x.Read())
            {
                value = (string)(x["ROWVALUE"]);
                valueString += value ;

                if (valueString.Length > 300)
                {
                    valueString= valueString.Replace(value , "");
                }
            }

valueString is not allowed to be over 300 characters in length, so when that buffer is hit, I am removing the last addition of value to keep it under 300.

My problem being, once I have reached the maximum length for valueString, I need to create a next valueString for the remaining values and repeat this process until all the values have been retrieved. How could I achieve this?

Foi útil?

Solução

You can store them in a list for example:

List<string> valueStrings = new List<string>();
string value, valueString;
while (x.Read())
{
    value = (string)(x["ROWVALUE"]);    

    if (valueString.Length + value.Length > 300)
    {
        valueStrings.Add(valueString);
        valueString = value;
    }
    else
    {
        valueString += value;
    }
}
valueStrings.Add(valueString); //don't forget about the last one
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top