码迷,mamicode.com
首页 > 数据库 > 详细

C# NPOI的数据批量导入数据库

时间:2017-01-22 17:48:22      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:http   上传文件   数据导入   导入   post   失败   for   成功   insert   

public ActionResult Upload(HttpPostedFileBase Namefile)
        {
            //判断文件是否存在
            if (Namefile == null)
            {
                return Content("没有文件");
            }
            //获取上传文件的名称,位置
            var fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + Namefile.FileName.Substring(Namefile.FileName.LastIndexOf(‘.‘));
            //执行上传
            Namefile.SaveAs(Server.MapPath("/FileExcel/" + fileName));
            //获取文件路径,读取数据
            string url = Server.MapPath("/FileExcel/" + fileName);
            //dt为读取到的所有Excel里的数据
            DataTable dt = ExcelHelper.ExcelImport(url);

    //循环dt里的数据
            foreach (DataRow item in dt.Rows)
            {

      string str="insert添加语句"; 

            }

     //执行SQL语句
            SqlCommand sqlcommand = new SqlCommand(str.ToString(), GetConn());
            //执行数据导入
           int q=  sqlcommand.ExecuteNonQuery();

    if(q>0)

    {

      return Content("<script>alert(‘上传已完成,您上传成功!‘);location.href=‘/FuLi/FuLiIndex‘</script>");

    }

    else

    {

      return Content("<script>alert(‘失败!‘);location.href=‘/FuLi/FuLiIndex‘</script>");

    }
        } 

 

C# NPOI的数据批量导入数据库

标签:http   上传文件   数据导入   导入   post   失败   for   成功   insert   

原文地址:http://www.cnblogs.com/liuyi-li/p/6340786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!