RT  我现在要批量的往数据库添加数据,数据是从别的网站上复制下来的,想用TXT文档,求解怎么用.NET读取TXT文档

解决方案 »

  1.   

    读取TXT文档并返回行数using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;using System.IO;
    using System.Text;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string Path = Server.MapPath("4.txt");
                insertStr(Path);
            }
        }    public void insertStr(string Path)
        {
            string strLine = "";
            int i = 0;
            try
            {
                StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312"));            strLine = sr.ReadLine();            while (strLine != null)
                {
                    Response.Write(strLine + "<br>");
                    strLine = sr.ReadLine();
                    i++;    
                }
                Response.Write(i);
                sr.Dispose();
                sr.Close();
            }
            catch 
            { 
            }       
        }}
      

  2.   

    string ss = File.ReadAllText("D:\\1.txt");
      

  3.   

    )创建txt文件【web.config】
       --------------------------------------------------------------------
    <appSettings>
        <add key="EditChars" value="D:\Site\ZJPS\TextFile\EditChars.txt"/>
    </appSettings>
    2) 页面的CS文件中:
       --------------------------------------------------------------------
    【1】 获取txt文件位置: protected string FileName = System.Configuration.ConfigurationManager.AppSettings["EditChars"].ToString();
       【2】 文件操作:创建、写、读 文件。
             #region 文件操作
            /// <summary>
            /// 创建txt文件
            /// </summary>
            public void CreateToFile()
            {
                StreamWriter SW;
                SW = File.CreateText(FileName);
                SW.Close();
            }
            /// <summary>
            /// 写文件
            /// </summary>
            public void WriteToFile()
            {
                string InsertStr = "";
                if (!File.Exists(FileName))
                {
                    CreateToFile();
                    InsertStr = "Content#DateTime#!";    //txt文件中用它来表示字段
                }
                InsertStr += this.txtContent.Text.ToString() + "#";
                InsertStr += Convert.ToString(Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd")) + "#";
                File.AppendAllText(FileName, InsertStr + "!", Encoding.BigEndianUnicode);
            }
            /// <summary>
            /// 读取文件
            /// </summary>
            /// <returns></returns>
            public string ReadTextFileDate()
            {
                string strInput = "";
                string GetStream = "";            if (File.Exists(FileName))
                {
                    StreamReader sr = new StreamReader(FileName, UnicodeEncoding.GetEncoding("gb2312"));
                    strInput = sr.ReadLine();
                    while (strInput != null)
                    {
                        GetStream += strInput;
                        strInput = sr.ReadLine();
                    }
                    sr.Close();
                }
                else
                {
                    lblContents.Text = "myFile.txt   does   not   exist!";
                }
                return GetStream;
            }
            #endregion/// <summary>    /// 修改txt中的内容/// </summary>public void UpdateTextFile(string FileName, string StrCount)    {        System.IO.StreamReader sr = new System.IO.StreamReader(FileName);        string s = sr.ReadToEnd();        sr.Close();
            s = s.Replace(s, StrCount);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(FileName, false);        sw.Write(s);        sw.Close();       }