• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

  /// <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);
               }

              
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap