zoukankan      html  css  js  c++  java
  • infopath表单中涉及从数据库中取值的函数实现

    最近在开发东莞国税网上办税报表系统,用到Infopath service作为表单服务器,后台数据存储采用Oracle,其中涉及了从数据库中取值写入Infopath 表单的需求,代码如下:

    /// <summary>
        /// 取本年累计
        /// </summary>
        /// <param name="strTableName">表名</param>
        /// <param name="xpn"></param>
        /// <param name="xnm"></param>
        /// <param name="bizDateBegin">业务期始</param>
        /// <param name="NSRSBH">纳税人号</param>
        /// <returns></returns>
        public static string GetSumValue(XPathNavigator xpn, XmlNamespaceManager xnm, string NSRSBH, int thisYear)
        {
            ILog log = LogManager.GetLogger("InfoPath");

            string SQL;
            //int thisyear = DateTime.Now.Year;
            SQL = @"select ";
            XPathNavigator Xpn = null;
            Xpn = xpn.SelectSingleNode("/my:root/my:SumValue1", xnm);
            string tablename = Xpn.GetAttribute(@"TableName", Xpn.NamespaceURI);


            List<string> Columns = new List<string>();
            if (xpn != null)
            {
                XPathNodeIterator itor = Xpn.SelectChildren(XPathNodeType.Element);
                int j = 0;

                while (itor.MoveNext())
                {

                    SQL += " sum(" + itor.Current.Name.Substring(0, itor.Current.Name.Length - 2).Replace("my:", "") + ") " + itor.Current.Name.Substring(0, itor.Current.Name.Length - 2).Replace("my:", "") + ",";

                    Columns.Add(itor.Current.Name.Replace("my:", ""));
                    j++;

                }

                SQL = SQL.Substring(0, SQL.Length - 1);
                SQL += " from " + tablename + " where  SSSQ_Q between to_date('" + thisYear.ToString() + "-1-1','YYYY-MM-DD') and to_date('" + thisYear.ToString() + "-12-31','YYYY-MM-DD') and NSRSBH='" + NSRSBH + "'";

                log.Info("[SQL]:" + SQL);

                OracleConnection conn = new OracleConnection(ConfigurationManager.AppSettings["FormDate_ConnectionString"]);
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    cmd.CommandText = SQL;
                    OracleDataReader odr = cmd.ExecuteReader();
                    if (odr.Read())
                    {
                        for (int i = 0; i < odr.FieldCount; i++)
                        {
                            NavSetValue(xpn, "/my:root/my:SumValue1/my:" + Columns[i], xnm, ((String.IsNullOrEmpty(odr[i].ToString())) ? "0" : odr[i].ToString()));
                        }

                    }
                    odr.Close();

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }

            }

            return SQL;

        }

    前台调用方式:

                int thisyear = Convert.ToInt32(Request.QueryString["bizDateBegin"].Substring(0, 4));
                Xpn = XmlFormView.XmlForm.MainDataSource.CreateNavigator().SelectSingleNode(Constants.DS_SUMVALUE, XmlFormView.XmlForm.NamespaceManager);
                    //是否有本年累计
                    if (Xpn != null)
                    {
                        InfopathHelper.GetSumValue(XmlFormView.XmlForm.MainDataSource.CreateNavigator(), XmlFormView.XmlForm.NamespaceManager, Request.QueryString["pid"], thisyear);
                    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    笔记-[5]-关于JS事件流!
    JS中的模块化开发之Sea.JS
    利用JavaScript如何创建一个table表格[第2种方法]
    二维数组左滑删除某一行
    sdwebimage缓存图片
    scrollView中内部控件的悬停
    (转)将图片保存到相册
    离屏渲染
    标签栏隐藏
    iOS8新特性之交互式通知
  • 原文地址:https://www.cnblogs.com/starcrm/p/1309808.html
Copyright © 2011-2022 走看看