上传txt文本文件,按行读取,存入数据库,怎么办呢??求教高手

时间:2022-12-22 14:51:44
ASP.net mvc开发
有一个txt文件,文件中内容格式如下:
031211248008
031211258004
031211258005
031211258012
031211258003
031211258019
031211258016
031211258011
031211258015
031211268014
031211268014
031211268017
031211268009
。。。。。。。
采用html的file属性将文件上传至服务器,然后按行读取上传上来的文件,在页面打印输出   应该怎么写啊??哪位大侠请指教。。。。。

4 个解决方案

#1



using(System.IO.StreamReader sr=    new System.IO.StreamReader(System.Web.HttpContext.Current.Request.Files["上传的file name"].InputStream))
{
                        string lien = sr.ReadLine();
                        //写入数据库操作.....
}

#2



                    //读取路径
                    string url = objclass.GetFieldById(batchid, "filePath");

                    //导入
                    System.IO.StreamReader sr = new System.IO.StreamReader(url);
                    String line = null;

                    if ((line = sr.ReadLine()) != null)
                    {
                      //.....................
                    }

#3



 private void button2_Click_1(object sender, EventArgs e)
       {
           SqlConnection conn = new SqlConnection(strConn);
           SqlTransaction tran = conn.BeginTransaction();
           String sql = "";
           String sLine = "";
           StreamReader objReader = new StreamReader(文件路径);
           ArrayList LineList = new ArrayList();

           try
           {
               while (sLine != null)
               {
                   sLine = objReader.ReadLine();
                   if (sLine != null && !sLine.Equals(""))
                       sql += "insert into 表名 values(" + "'" + sLine + "'" + ")  ";
               }
               objReader.Close();
               conn.Open();
               SqlCommand cmd = new SqlCommand(sql, conn);
               cmd.Transaction = tran;
               cmd.ExecuteNonQuery();
               tran.Commit();
           }
           catch
           {
               tran.Rollback();
               MessageBox.Show("Error");  
               return; 
           }
           finally {
               conn.Close();
           }

#4


 using System.IO;
       private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ttxt = new OpenFileDialog();
            ttxt.Filter = "TXT文件 (*.txt)|*.txt";
            ttxt.ShowDialog();
            if (ttxt.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ttxt.FileName;
               // StreamReader sr = new StreamReader(textBox1.Text);


            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(System.Int32));
            dt.Columns.Add("Code", typeof(System.String));
            int ii = 0;
            StreamReader sr = new StreamReader(textBox1.Text);
            
            while ((sr.Read()>0))
            {

                string lien = sr.ReadLine();
                DataRow dr = dt.NewRow();
                dr[0] = ii.ToString(); ;
                dr[1] = lien;
                dt.Rows.Add(dr);
                ii++;
            }
                dataGridView1.DataSource = dt;
           
        }

#1



using(System.IO.StreamReader sr=    new System.IO.StreamReader(System.Web.HttpContext.Current.Request.Files["上传的file name"].InputStream))
{
                        string lien = sr.ReadLine();
                        //写入数据库操作.....
}

#2



                    //读取路径
                    string url = objclass.GetFieldById(batchid, "filePath");

                    //导入
                    System.IO.StreamReader sr = new System.IO.StreamReader(url);
                    String line = null;

                    if ((line = sr.ReadLine()) != null)
                    {
                      //.....................
                    }

#3



 private void button2_Click_1(object sender, EventArgs e)
       {
           SqlConnection conn = new SqlConnection(strConn);
           SqlTransaction tran = conn.BeginTransaction();
           String sql = "";
           String sLine = "";
           StreamReader objReader = new StreamReader(文件路径);
           ArrayList LineList = new ArrayList();

           try
           {
               while (sLine != null)
               {
                   sLine = objReader.ReadLine();
                   if (sLine != null && !sLine.Equals(""))
                       sql += "insert into 表名 values(" + "'" + sLine + "'" + ")  ";
               }
               objReader.Close();
               conn.Open();
               SqlCommand cmd = new SqlCommand(sql, conn);
               cmd.Transaction = tran;
               cmd.ExecuteNonQuery();
               tran.Commit();
           }
           catch
           {
               tran.Rollback();
               MessageBox.Show("Error");  
               return; 
           }
           finally {
               conn.Close();
           }

#4


 using System.IO;
       private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ttxt = new OpenFileDialog();
            ttxt.Filter = "TXT文件 (*.txt)|*.txt";
            ttxt.ShowDialog();
            if (ttxt.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ttxt.FileName;
               // StreamReader sr = new StreamReader(textBox1.Text);


            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(System.Int32));
            dt.Columns.Add("Code", typeof(System.String));
            int ii = 0;
            StreamReader sr = new StreamReader(textBox1.Text);
            
            while ((sr.Read()>0))
            {

                string lien = sr.ReadLine();
                DataRow dr = dt.NewRow();
                dr[0] = ii.ToString(); ;
                dr[1] = lien;
                dt.Rows.Add(dr);
                ii++;
            }
                dataGridView1.DataSource = dt;
           
        }