1 获取xml文件

XmlDocument xDoc = this.GetUploadData();

private XmlDocument GetUploadData()
  {
   XmlDocument xmlDoc;

HttpPostedFile pf = fileSelect.PostedFile;
   if(pf.FileName == "")
   {
    lb_Message.Text = "请选择上传文件!";
    return null;
   }
   byte[] buffer= new byte[pf.ContentLength];
   Stream st = pf.InputStream;
   st.Read(buffer,0,pf.ContentLength);

string xmlContent = Encoding.Default.GetString(buffer);

try
   {
    xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlContent);
    return xmlDoc;
   }
   catch
   {
    return null;
   }
  }

2 得到各个结点的值

XmlNodeList xmlNL = xDoc.GetElementsByTagName("recordset");
    string ReportType = xmlNL[0].Attributes["ModelCode"].Value.Trim();
    string AgentID = Session["_PRCS_USERID"].ToString().Trim();

3 插入数据库

xmlNL = xDoc.GetElementsByTagName("row");
      if (xmlNL.Count > 0)
      {
       strSql = "";
       foreach (XmlNode xn in xmlNL)
       {
        string ProductLine = xn.Attributes["ProductLine"].Value;
        string ProductSeries = xn.Attributes["ProductSeries"].Value;
        string ProductModel = xn.Attributes["ProductModel"].Value;
        string SInNum =   xn.Attributes["SInNum"].Value;
        string HalfwayNum =  xn.Attributes["HalfwayNum"].Value;
        string InvNum =   xn.Attributes["InvNum"].Value;
        string SOutNum =  xn.Attributes["SOutNum"].Value;

strSql += "INSERT INTO [dbo].[RSKPI_UploadDetail]([AgentID], [ReportType], [UploadTime], [ProductLine], [ProductSeries], [ProductModel], [SInNum], [HalfwayNum], [InvNum], [SOutNum]) "+
         "VALUES('"+AgentID+"', '"+ReportType+"', getdate(), '"+ProductLine+"', '"+ProductSeries+"', '"+ProductModel+"', "+SInNum+", "+HalfwayNum+", "+InvNum+", "+SOutNum+"); ";
       }
       SqlHelper.ExecuteNonQuery(dbConn, CommandType.Text, strSql);
       this.lb_Message.Text = "数据上报成功!";

源代码:

XmlDocument xDoc = this.GetUploadData();
   if (xDoc != null)
   {
    XmlNodeList xmlNL = xDoc.GetElementsByTagName("recordset");
    string ReportType = xmlNL[0].Attributes["ModelCode"].Value.Trim();
    string AgentID = Session["_PRCS_USERID"].ToString().Trim();
    string strSql = "select count(*) from dbo.RSKPI_AgentPermission where AgentID='"+AgentID+"' and ReportType='"+ReportType+"'";
    if (SqlHelper.ExecuteScalar(dbConn, CommandType.Text, strSql).ToString() != "0")
    {
     strSql = "select parValue from dbo.RSKPI_Parameter where ParKey='"+ReportType+"'";
     string version1 = SqlHelper.ExecuteScalar(dbConn, CommandType.Text, strSql).ToString();
     string version2 = xmlNL[0].Attributes["EditionNum"].Value.Trim();
     if (version1 == version2)
     {
      //清除当天的上传
      strSql = "delete from RSKPI_UploadDetail where AgentID='"+AgentID+"' and ReportType = '"+ReportType+"' and UploadTime > convert(datetime,convert(varchar(10),getdate(),120)) and UploadTime < convert(datetime,convert(varchar(10),getdate()+1,120))";
      SqlHelper.ExecuteNonQuery(dbConn, CommandType.Text, strSql);

xmlNL = xDoc.GetElementsByTagName("row");
      if (xmlNL.Count > 0)
      {
       strSql = "";
       foreach (XmlNode xn in xmlNL)
       {
        string ProductLine = xn.Attributes["ProductLine"].Value;
        string ProductSeries = xn.Attributes["ProductSeries"].Value;
        string ProductModel = xn.Attributes["ProductModel"].Value;
        string SInNum =   xn.Attributes["SInNum"].Value;
        string HalfwayNum =  xn.Attributes["HalfwayNum"].Value;
        string InvNum =   xn.Attributes["InvNum"].Value;
        string SOutNum =  xn.Attributes["SOutNum"].Value;

strSql += "INSERT INTO [dbo].[RSKPI_UploadDetail]([AgentID], [ReportType], [UploadTime], [ProductLine], [ProductSeries], [ProductModel], [SInNum], [HalfwayNum], [InvNum], [SOutNum]) "+
         "VALUES('"+AgentID+"', '"+ReportType+"', getdate(), '"+ProductLine+"', '"+ProductSeries+"', '"+ProductModel+"', "+SInNum+", "+HalfwayNum+", "+InvNum+", "+SOutNum+"); ";
       }
       SqlHelper.ExecuteNonQuery(dbConn, CommandType.Text, strSql);
       this.lb_Message.Text = "数据上报成功!";

转载于:https://www.cnblogs.com/yuanweisen/archive/2009/03/03/1402436.html

将XML文件数据插入到数据库中相关推荐

  1. html数据插入到数据库,将数据插入到数据库中:text和html格式

    将数据(文本和HTML格式)插入到mysql字段LONGTXT中时出现问题. 这里是错误将数据插入到数据库中:text和html格式 public 'error' => string 'You ...

  2. excel文件数据导入mysql数据库中_将excel里面的数据导入mysql数据库中

    展开全部 条件:PC端已经安装Navicat工具,并636f70793231313335323631343130323136353331333363386161且已经成功连接至数据库. 1.点击鼠标右 ...

  3. 批量插入数据库语句java_java相关:MyBatis批量插入数据到Oracle数据库中的两种方式(实例代码)...

    java相关:MyBatis批量插入数据到Oracle数据库中的两种方式(实例代码) 发布于 2020-7-22| 复制链接 本文通过实例代码给大家分享了MyBatis批量插入数据到Oracle数据库 ...

  4. mybatis insert 插入数据,显示执行成功,但未真正插入到数据库中

    (1)通过mybatis插入数据库,插入数据,显示插入成功,查询数据库,发现并未插入成功.通过日志可看到原因为:插入的语句回滚连接数据库了,导致插入失败. (2)应加入commit语句,手动执行提交事 ...

  5. java 农历的编写保存数据批量插入到数据库中

    java 农历的编写保存数据批量插入到数据库中 1.阴历转农历的工具类 public class Nongli {private static int monCyl, dayCyl, yearCyl; ...

  6. 【随记】Python:前端表格获取到的填写数据插入到数据库表格中数据类型问题

    Python:前端表格获取到的填写数据插入到数据库表格中数据类型问题 背景 问题再现 结论 背景 用户在前端界面的表格中填写数据,通过 text() 获取到的数据插入到数据库表中,该过程涉及到了数据类 ...

  7. html表格中绑定显示xml文档内容的简单实例,JS读取XML文件数据并以table形式显示数据的方法(兼容IE与火狐)...

    本文实例讲述了JS读取XML文件数据并以table形式显示数据的方法.分享给大家供大家参考,具体如下: 先看xml文件: 张秋丽 女 18 李文才 男 31 李斯文 男 22 马英 女 25 孙红雷 ...

  8. php中怎么连接数据库中的表,php 连接 excel表格数据库数据-php中如何将execl的数据导入到数据库中...

    php中如何将execl的数据导入到数据库中 php导出大量数据的Excel: PHP从数据库分多次读取100万行记录,和分多次将100万行写入文本文件都没问题 Excel可以支持100万行记录,Ex ...

  9. list mybatis批量保存_mybatis 批量将list数据插入到数据库的实现

    随着业务需要,有时我们需要将数据批量添加到数据库,mybatis提供了将list集合循环添加到数据库的方法.具体实现代码如下: 1.mapper层中创建 insertForeach(List < ...

最新文章

  1. (翻译)Quartz官方教程——第七课:TriggerListeners 和 JobListeners
  2. tomcat源码之架构解析
  3. sounds speech_speech sounds
  4. [BX]和loop指令03 - 零基础入门学习汇编语言25
  5. HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)
  6. 掌握SpringAOP
  7. php qq邮箱发送邮件报错_PHP实现发送邮件(二)
  8. 域名后面加端口号_第一节.外贸网站如何做好域名选择-外贸网站营销建站及推广...
  9. 201809-2-买菜
  10. Stream进阶篇-消费组实现验证
  11. DirectX组件和安装在系统上驱动程序
  12. 8个SVG动画JavaScript库
  13. wordpress seo设置全套SEO插件教程
  14. 计算机管理禁用usb,电脑如何禁用U盘、怎样禁用USB存储工具,防止USB端口泄密?...
  15. javajavaweb学习笔记(汇总)
  16. 三次样条插值详解(附代码实现)
  17. 自然语言处理NLP基础知识
  18. 挺带劲!这款免费开源的监控系统真强大~
  19. RDKit分子的3D结构
  20. 计算机硬件有问题怎么办,我电脑发现网络硬件配置存在问题,需要修复怎么办...

热门文章

  1. IT程序员在北京可以选择哪些国企、央企以及研究所?
  2. stream().map().collect()用法
  3. 不融资,如何在大数据BI市场占据一席之地
  4. destoon php os,destoon运行流程二次开发必看
  5. 小米8吃鸡战斗服务器响应超时,小米8使用1天真实体验,看完再决定买不买?
  6. linux判断是否能上网_如何判断飘窗是否能砸掉?砸掉飘窗可以怎么利用?
  7. python命令行进入帮助模式_Python 命令行之旅:深入 click 之选项篇
  8. 路由器下一跳地址怎么判断_路由器的功能及工作原理
  9. 数据结构 7-0 查找
  10. *1LL在c++中的意义