各位大哥,请问用C#写过读取.txt文件内容,帮帮忙了,谢谢! txt 文件目录为任意,通过控件选择,<INPUT id="myFile" type="file" size="14" name="myFile" Runat="Server">然后通过控件<asp:ImageButton id="fileImportB" runat="server" ImageUrl="../images/star_26.gif">导入文件内容格式为:
13888000001,一
13888000002,二要求将txt文件内容中满足条件的内容全部读取,并插入数据库
小弟以前没写过,望各位帮忙

解决方案 »

  1.   

    读取
    if(filePath.PostedFile.FileName != "")
    {
    txtFilePath =filePath.PostedFile.FileName;
    fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".")+1,3); if(fileExtName !="txt" && fileExtName != "TXT")
    {
    Response.Write("请选择文本文件");
    }
    else
    {
    StreamReader fileStream = new StreamReader(txtFilePath,Encoding.Default);
    txtContent.Text = fileStream.ReadToEnd();
    fileStream.Close();
    }
    }
      

  2.   

    OpenFileDialog dig = new OpenFileDialog();
                dig.Filter = "文本文档(*.txt)|*.txt|所有文件(*.*)|*.*";
                if (dig.ShowDialog() == DialogResult.OK)
                {
                    StreamReader reader = new StreamReader(new FileStream(dig.FileName, FileMode.Open), System.Text.Encoding.Default);
                    string Str = reader.ReadToEnd();
                    reader.Close();
                    textBox1.Text = Str; \\这里可以写什么什么什么的什么 = str
      

  3.   

    插入库的话  可以做一个字符串 = str然后用insert插入
      

  4.   

    13888000001,一
    13888000002,二
    固定格式么?固定格式:要求将txt文件内容中满足条件的内容全部读取,并插入数据库    比较好操作非固定格式
    blackhero(黑侠客)  用Excel
      

  5.   

    问题已解决,谢谢各位,txt文件格式是固定的,以后将会改进用Excel