我的streamreader一次只读一行

时间:2014-08-10 08:47:44

标签: c# asp.net streamreader

string UploadFile = Server.MapPath("~/Uploads/");
UploadFile += FileUpload1.FileName;`
try
{
    if (File.Exists(UploadFile))
    {
        File.Delete(FileUpload1.FileName);
    }
    else
    {
        FileUpload1.PostedFile.SaveAs(UploadFile);
    }
}

这就是我最后找到了我的While循环的地方..

finally
{
    string InputLine, tempSTR, productname, strQuantity, strUnitprice;
    StreamReader ReadFile = new StreamReader(UploadFile);

    while (!ReadFile.EndOfStream)
    { 

        InputLine = ReadFile.ReadLine();

        tempSTR = InputLine.Substring(0, InputLine.IndexOf(","));
        productname = tempSTR;
        InputLine = InputLine.Substring(InputLine.IndexOf(",") + 1).Trim();

        tempSTR = InputLine.Substring(0, InputLine.IndexOf(","));
        strQuantity = tempSTR;
        InputLine = InputLine.Substring(InputLine.IndexOf(",") +1).Trim();

        strUnitprice = InputLine;

        b.NAME = productname;
        b.QUANTITY = int.Parse(strQuantity);
        b.UNITPRICE = int.Parse(strUnitprice);
        b.AddProducts();
    }

    GridView1.DataSource = b.ViewProducts();
    GridView1.DataBind();
}

在我的while循环中,当我尝试上传以下几行时出现错误:

Acer P166HQL LED显示器,97,2999
   Acer S200HQL LED显示器,50,4499
   戴尔UltraSharp 2005FPW液晶显示器,22,29999
   Gateway KX1563 LED显示器,66,2988

1 个答案:

答案 0 :(得分:0)

我不知道你的文件结构,但我认为如果你这样使用解析,你就不会有问题了:

finally
{

    string InputLine, tempSTR, productname, strQuantity, strUnitprice;
    StreamReader ReadFile = new StreamReader(UploadFile);
    string fileContent = ReadFile.ReadToEnd();
    fileContent = fileContent.Replace("\r\n",",");
    string[] splitedText = fileContent .Split(',');
    int j = 0;
    for(int i = 0;i < splitedText.Length ; i++)
    {
      switch(j)
      {
         case 0:
          j++;
          b.NAME = splitedText[i];
         break;
         case 1:
          j++;
          b.QUANTITY = int.Parse(splitedText[i]);
         break;
         case 2:
          j=0;
          b.UNITPRICE = int.Parse(splitedText[i]);
          b.AddProducts();
         break;
      }
    }
    GridView1.DataSource = b.ViewProducts();
    GridView1.DataBind();

}

<强>更新

我根据您的评论更改了整个代码:

  string s = @" Acer P166HQL LED Monitor, 97, 2999 ASRock Penryn 1600SLi, 42, 4688 Gateway KX1563 LED Monitor, 66, 2988";
            string[] splitedText = s.Split(' ');
            StringBuilder sr = new StringBuilder();
            int countOfCommas = 0;
            bool needComma = false;
            foreach (string item in splitedText)
            {
                if (needComma)
                {
                    needComma = false;
                    sr.Append(item).Append(',').Append(' ');
                }
                else if (item.Contains(","))
                {
                    if (countOfCommas == 1)
                    {
                        countOfCommas = 0;
                        needComma = true;
                    }
                    else
                    {
                        countOfCommas++;
                    }
                    sr.Append(item).Append(' ');
                }
                else
                {
                    sr.Append(item).Append(' ');
                }
            }

            string str = sr.ToString();
            string[] splitedTextByComma = str.Split(',');
            int j = 0;
            for (int i = 0; i < splitedTextByComma.Length; i++)
            {
                switch (j)
                {
                    case 0:
                        j++;
                        string NAME = splitedTextByComma[i];
                        break;
                    case 1:
                        j++;
                        int QUANTITY = int.Parse(splitedTextByComma[i]);
                        break;
                    case 2:
                        j = 0;
                        int UNITPRICE = int.Parse(splitedTextByComma[i]);
                        break;
                }
            }
        }

s变量有一些你说过的值。我希望这会有所帮助。

相关问题