asp.net 导入文本文件数据到数据库

  /// <summary>
    /// 创建并获取目录,主要用于用户文件上传的目录,目录创建在resources/upload下
    /// </summary>
    /// <param name="customize">自定义类别可以是空NULL,此参数不为空时在upload下会再新建一层目录,如:adminPic则返回../resources/upload/adminPic/年/月/</param>
    /// <returns>../resources/upload/adminPic/年/月/</returns>
    public static string getAndCreatePath(string customize)
    {
        string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/resources/upload/" + ((customize == string.Empty) ? (customize.ToString() + "/") : ("")) + 

DateTime.Now.Year + "/" + DateTime.Now.Month + "/");
        try
        {
            if (!File.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path;
        }
        catch (Exception)
        {
            return HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["errorPath"]).ToString();
        }
    }
    /// <summary>
    /// 文本数据导入
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void But_importChinaBank_Click(object sender, EventArgs e)
    {
       
       if (flChinaBank.PostedFile.FileName == "")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "1", "<script>alert('导入的文本文件不存在!');</script>");

        }
        else
        {

            string FilePath = getAndCreatePath("");
            string FileName = DateTime.Now.Ticks.ToString();
            //string tableName;
            string postFileName;//后缀名
            string newName;
            try
            {
                int pos = flChinaBank.PostedFile.FileName.LastIndexOf(".") + 1;
                postFileName = flChinaBank.PostedFile.FileName.Substring(pos, flChinaBank.PostedFile.FileName.Length - pos);//后缀名
                newName = FilePath + FileName + "." + postFileName;
                flChinaBank.PostedFile.SaveAs(newName);
            }
            catch (Exception exec)
            {
                throw (exec);
            }

               FileStream fs = new FileStream(newName, FileMode.Open, FileAccess.Read);
                StreamReader m_streamReader = new StreamReader(fs);
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string value = m_streamReader.ReadLine();
                string[] stringSeparators = new string[] { " " };
                string sql = "";
                int nonquery = 0;
                for (int i = 0; value != null; i++)
                {
                   
                    //value = value.Replace(",", "");
                    string[] array = value.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);

                    sql += string.Format("insert into L_POSImportInfo_Share values('{0}','{1}','{2}','{3}','{4}','{5}',{6},{7},{8},'{9}','{10}','{11}','{12}','{13}','{14}','{15}'); ",
                        0, 104370157225027, array[0].ToString(), (array[3].Substring(4, 4) + array[4]).ToString(), array[2], "", double.Parse(array[5].Replace(",", "").ToString()) * 100.00,

    double.Parse(array[6].ToString()) * 100.00, double.Parse(array[7].ToString()) * 100.00, "", double.Parse(array[8].ToString()) * 100.00, "", "", "", 0,DateTime.Now.ToString("s"));
                   
                }
                nonquery = baser.ExecuteSQL(sql);
                if (nonquery > 0)
                {
                  
                     
                     Page.ClientScript.RegisterStartupScript(this.GetType(),Guid.NewGuid().ToString(), "<script>alert('导入成功');</script>");
                     GridView1.EditIndex = -1;
                     dataDind(0);
                }
                else
                {
                    
                    Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script>alert('导入失败!');</script>");

                    
                }
                m_streamReader.Close();
                fs.Close();
               
                //删除上传的文件
               if (File.Exists(newName))
               {
                  File.Delete(newName);
               }

              
        }
    }
}
原文地址:https://www.cnblogs.com/wangyuelang0526/p/2566321.html