gdufo

 

InfoPath 与 数据库的通讯


http://www.cnblogs.com/sumh/archive/2008/03/27/1123903.html

一、在
vs中创建表单模板把数据存到数据库中。

新建infopath表单模板:

打开vs2008,新建Project,在Project Types 区中选择 Visual C#àOffice-à2007--àInfoPathFormTemplate ,在Name中填入infopath表单模板的名称,如PurcharForm;如下图:


 

开始设计表单:

1.         点击右边“设计任务”中的控件,利用控件设计表单;

2.         添加数据源,点击右边“设计任务”中的数据源,选择“管理数据连接”,点击添加按钮,在新建数据源中,选择“仅接收数据”点击下一步,希望从何处接收数据,选择“数据库”,点击下一步,点击“选择数据库”点击新建数据源,然后一直点击下一步。

3.         绑定重复表的数据源,选择要显示的字段。

4.         提交按钮创建规则,选择“使用数据连接提交”,然后选择“提交到宿主环境”。

5.         表单设计好了,开始“检查设计方案”设置兼容性选中“设计一个可在浏览器或InfoPath中可以打开的表单模板”,安全级别选择“完全信任”。

6.         保存模板。


7. 创建信息初始化列表“WFMetaData”,标题“申请单编号确认规则”创建栏“PurcharIDFormat0”值是“P-,yyyyMMddHHmmA”。用这种规则创建计划单编号。

8. Insert -----àLoading 事件,先创建数据源,读取初始化信息,代码如下:

public void FormEvents_Loading(object sender, LoadingEventArgs e)

        {

            try

            {

                //取到提交连接的对象,FileSubmitConnection对应的是提交到SharePoint文档库的连接类

                FileSubmitConnection osssubmit = (FileSubmitConnection)this.DataConnections["OSSSubmit"];

                XPathNavigator fieldFillAuthor = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:制表人", NamespaceManager);

                XPathNavigator fieldFillID = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:计划单编号", NamespaceManager);

                //根据提交连接的FolderUrl属性可以取到文档库所在的网站集 (有些情况下工作流网站并不在Root站点上,所以不适合此处)

                SPSite mysite = new SPSite(osssubmit.FolderUrl);

                string PurcharWebName = ConfigurationManager.AppSettings["PurcharWebName"];

                //根据提交连接的FolderUrl属性可以取到文档库所在的网站(直接得到当前网站)

                using (SPWeb myweb = mysite.OpenWeb(PurcharWebName)) // 实际中需要更改为OpenWeb("/wf/")

                {

                    if (fieldFillAuthor != null)//自动赋值填表人,格式为 "中文显示名(域帐号)"

                    {

                        fieldFillAuthor.SetValue(myweb.CurrentUser.Name + "(" + myweb.CurrentUser.LoginName + ")");

                    }

                    SPList idList = myweb.Lists["WFMetaData"];

                    string idFormat = "";

                    string idFormat2 = "";

                    foreach (SPListItem item in idList.Items)

                    {

                        if (item.Title == "申请单编号确认规则")

                        {

                            idFormat = item["PurcharIDFormat0"].ToString().Split(new char[] { ',' })[0];

                            idFormat2 = item["PurcharIDFormat0"].ToString().Split(new char[] { ',' })[1];

                            idFormat = idFormat+ DateTime.Now.ToString(idFormat2);

                            break;

                        }

                    }

                    if (fieldFillID != null) //申请单编号确认规则

                    {

                        fieldFillID.SetValue(idFormat);

                    }

                }

            }

            catch

            { }

填写好信息将信息数据系列化到数据库中,创建了FormSave方法把重复表数据系列化道数据库中和PurcharToDB方法把申请单主数据系列化到数据库中,代码如下:

///<summary>

        ///把重复表数据序列化至数据库

        ///</summary>

        private void FormSave()

        {

            // 在此处编写代码。

            string strResult = "";

            int currentPid = -1;

            try

            {

                //取到提交连接的对象,FileSubmitConnection对应的是提交到SharePoint文档库的连接类

                FileSubmitConnection osssubmit = (FileSubmitConnection)this.DataConnections["OSSSubmit"];

                //根据提交连接的FolderUrl属性可以取到文档库所在的网站集 (有些情况下工作流网站并不在Root站点上,所以不适合此处)

                SPSite mysite = new SPSite(osssubmit.FolderUrl);

                //读取列表的记录

                string PurcharListName = ConfigurationManager.AppSettings["MEPPurcharListName"];

                string PurcharWebName = ConfigurationManager.AppSettings["PurcharWebName"];

                //计划单编号

                string PurcharID = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:计划单编号", this.NamespaceManager).Value;

                using (SPWeb myweb = mysite.OpenWeb(PurcharWebName)) // 实际中需要更改为OpenWeb("/wf/")

                {

                    //存储数据到数据库中

                    currentPid = PurcharToDB(myweb.CurrentUser.LoginName, myweb.CurrentUser.Name);

                    //遍历重复表group5中的每一项

                    XPathNavigator NodeIter = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:group5", this.NamespaceManager);

                    //定义操作字串

                    string strBatch = "";

                    SPList list = myweb.Lists[PurcharListName];

                    foreach (XPathNavigator myf in NodeIter.SelectChildren(XPathNodeType.Element))

                    {

                        //对于重复表中的每一行,到数据库去查找,如果找到就使用Update,找不到就使用New

                        PurcharData purData = new PurcharData();

                        purData.ID = -1;

                        purData.PID = currentPid;

                        purData.Number = myf.SelectSingleNode("my:序号", NamespaceManager).ValueAsInt;

                        purData.SBName = myf.SelectSingleNode("my:物资名称", NamespaceManager).Value;

                        purData.SBType = myf.SelectSingleNode("my:规格型号", NamespaceManager).Value;

                        purData.Shuliang = (float)myf.SelectSingleNode("my:数量", NamespaceManager).ValueAsDouble;

                        purData.Address = myf.SelectSingleNode("my:生产厂", NamespaceManager).Value;

                        purData.GetTime = myf.SelectSingleNode("my:到货时间", NamespaceManager).ValueAsDateTime;

                        purData.UnitPrice = (float)myf.SelectSingleNode("my:预算单价", NamespaceManager).ValueAsDouble;

                        purData.Totals = (float)myf.SelectSingleNode("my:预算总价", NamespaceManager).ValueAsDouble;

                        purData.Notes = myf.SelectSingleNode("my:备注", NamespaceManager).Value;

                        purData.Results = myf.SelectSingleNode("my:执行情况记录", NamespaceManager).Value;

                        purData.Created = DateTime.Now;

                        purData.AuthorId = myweb.CurrentUser.LoginName;

                        purData.AuthorName = myweb.CurrentUser.Name;

                        purData.JHDID = PurcharID;

                        wf_PurcharData wfPurData = new wf_PurcharData();

                        if (wf_PurcharData.Exists(PurcharID, purData.Number,purData.SBName,purData.SBType))

                        {

                            //如果找到了就更新

                            wfPurData.Update(purData);

                        }

                        else

                        {

                            //如果没有找到就新增

                            wfPurData.Add(purData);

                        }

                    ////下面检查,此次表单编辑,是否删除了记录

             

                    //    if (!stillExist)

                    //    {//如果老项已经在新的编辑过程中删除了,那么就执行删除Method

                    //        strBatch += "<Method ID='" + MethodId.ToString() + "' Cmd='Delete'><Field Name='ID'>" + item.ID.ToString() + "</Field></Method>";

                    //    }

                    }

                }//using 结束

            }

            catch{}

        }

        ///<summary>

        ///将申请单主数据序列化到数据库中

        ///</summary>

        ///<param name="userID">域帐号</param>

        ///<param name="userName">显示名</param>

        ///<returns></returns>

        private int PurcharToDB(string userID, string userName)

        {

            try

            {

                XPathNavigator xpath = this.MainDataSource.CreateNavigator();

                if (xpath != null)

                {

                    XPathNavigator fieldJHDID = xpath.SelectSingleNode("/my:myFields/my:计划单编号", NamespaceManager);

                    XPathNavigator fieldZK = xpath.SelectSingleNode("/my:myFields/my:转口", NamespaceManager);

                    XPathNavigator fieldGC = xpath.SelectSingleNode("/my:myFields/my:国产", NamespaceManager);

                    XPathNavigator fieldSBXC = xpath.SelectSingleNode("/my:myFields/my:设备现场采购", NamespaceManager);

                    XPathNavigator fieldWZGN = xpath.SelectSingleNode("/my:myFields/my:物资国内采购", NamespaceManager);

                    XPathNavigator fieldWZXC = xpath.SelectSingleNode("/my:myFields/my:物资现场采购", NamespaceManager);

                    XPathNavigator fieldCountry = xpath.SelectSingleNode("/my:myFields/my:项目所在国", NamespaceManager);

                    XPathNavigator fieldProjectName = xpath.SelectSingleNode("/my:myFields/my:项目名称", NamespaceManager);

                    XPathNavigator fieldBeginTime = xpath.SelectSingleNode("/my:myFields/my:开工日期", NamespaceManager);

                    XPathNavigator fieldFillTime = xpath.SelectSingleNode("/my:myFields/my:填表日期", NamespaceManager);

                    XPathNavigator fieldNumPersonId = xpath.SelectSingleNode("/my:myFields/my:contact/my:Person/my:AccountId", NamespaceManager);

                    XPathNavigator fieldNumPersonName = xpath.SelectSingleNode("/my:myFields/my:contact/my:Person/my:DisplayName", NamespaceManager);

                    XPathNavigator fieldFirstPersonId = xpath.SelectSingleNode("/my:myFields/my:contacter/my:Person/my:AccountId", NamespaceManager);

                    XPathNavigator fieldFirstPersonName = xpath.SelectSingleNode("/my:myFields/my:contacter/my:Person/my:DisplayName", NamespaceManager);

                    XPathNavigator fieldInitComment = xpath.SelectSingleNode("/my:myFields/my:comment", NamespaceManager);

                    XPathNavigator fieldTotalsMoney = xpath.SelectSingleNode("/my:myFields/my:合计", NamespaceManager);

                    Purchar purchar = new Purchar();

                    purchar.PID = -1;

                    purchar.JHDID = fieldJHDID.Value;

                    purchar.ZK = fieldZK.ValueAsBoolean;

                    purchar.GC = fieldGC.ValueAsBoolean;

                    purchar.SBXC = fieldSBXC.ValueAsBoolean;

                    purchar.WZGN = fieldWZGN.ValueAsBoolean;

                    purchar.WZXC = fieldWZXC.ValueAsBoolean;

                    purchar.Country = fieldCountry.Value;

                    purchar.ProjectName = fieldProjectName.Value;

                    purchar.BeginTime = fieldBeginTime.ValueAsDateTime;

                    purchar.FillTime = fieldFillTime.ValueAsDateTime;

                    purchar.FillPersonId = userID;

                    purchar.FillPersonName = userName;

                    purchar.NumPersonId = fieldNumPersonId.Value;

                    purchar.NumPersonName = fieldNumPersonName.Value;

                    purchar.FirstPersonId = fieldFirstPersonId.Value;

                    purchar.FirstPersonName = fieldFirstPersonName.Value;

                    purchar.InitComment = fieldInitComment.Value;

                    purchar.TotalsMoney = (float)fieldTotalsMoney.ValueAsDouble;//Double 类型

                    wf_Purchar wfPurchar = new wf_Purchar();

                    return wfPurchar.Add(purchar);

                }

                else

                {

                    return -1;

                }

            }

            catch (Exception)

            {

                return -1;

            }

        }

9. 提交按钮事件中,调用FormSave()方法,保存到数据库中。

10.              到最后一步了,将它发布到InfoPath Form Services SharePoint 服务器

11.上传到管理中心,应用程序管理--àInfoPath Form Services---à配置 InfoPath Form  Services----à选中嵌入式SQL身份验证和数据源的身份验证(用户表单模板),

应用程序管理--àInfoPath Form Services---à配置 InfoPath Form Services----à上传表单模板 ,激活到网站集中,

12.新建表单库---〉设置表单库-----〉高级设置---内容类型设置为允许管理内容类型----〉启用了浏览器的文档设置为显示为页面。

内容类型----〉从现有网站内容类型添加------〉添加你上传的表单模板

二、在office中创建表单模板读取数据库中的值。

1.         Office ---àMicrosoft office InfoPath 2007-----à设计表单模板---------à空白----------à确定

2.         开始设计表单:

点击右边“设计任务”中的控件,利用控件设计表单;

添加数据源,点击右边“设计任务”中的数据源,选择“管理数据连接”,点击添加按钮,在新建数据源中,选择“仅接收数据”点击下一步,希望从何处接收数据,选择“数据库”,点击下一步,点击“选择数据库”点击新建数据源,然后一直点击下一步。

绑定重复表的数据源,选择要显示的字段。

提交按钮创建规则,选择“使用数据连接提交”,然后选择“提交到宿主环境”。

表单设计好了,开始“检查设计方案”设置兼容性选中“设计一个可在浏览器或InfoPath中可以打开的表单模板”,安全级别选择“完全信任”。

保存模板。

工具------à编程-----àLoading事件,添加代码如下:

            try

            {

                // 在此处编写代码。

                //数据源关联

                AdoQueryConnection myAdoQueryConnection =

                   (AdoQueryConnection)(this.DataConnections["wf_PurcharData"]);

                // 获取查询字段

                XPathNavigator wfNum =

                   CreateNavigator().SelectSingleNode("/my:myFields/my:wfNum",

                   NamespaceManager);

                //字段值

                string wfNumID = wfNum.InnerXml;

                //使用关键字段进行数据库查询

                string tmpConn = myAdoQueryConnection.Command;

                myAdoQueryConnection.Command = tmpConn + " " + "where ""JHDID"" = '" + wfNumID + "'";

                myAdoQueryConnection.Execute();

            }

            catch { }

设计好表单,如下:

 

预览表单。


 

发布到网络指定的位置。

web.config
在 appSettings节点,connectionStrings节点加上下面这些内容:

<appSettings>        
          <add key="MEPPurcharListName" value="物质采购申请单" />  
   <add key="PurcharWebName" value="/wf" />
   <add key="gsegc_ConnectionString" value="Password=sa;User ID=sa;Data

Source=litware;Initial Catalog=AD;"/>
   <add key="PowerUser" value="administrator" />
   <add key="PowerPwd" value="sa" />
   <add key="PowerDomain" value="litware" />

  </appSettings>

<connectionStrings>   
  <add name="GSEGC_ConnectionString" connectionString="Password=sa;User

ID=sa;Data Source=litware;Initial Catalog=AD;" providerName="System.Data.SqlClient"/>
  <add name="GSEGC_LogConnectionString" connectionString="Password=sa;User

ID=sa;Data Source=litware;Initial Catalog=AD;" providerName="System.Data.SqlClient"/>
 </connectionStrings>

posted on 2009-11-07 12:38 gdufo 阅读(821) 评论(0)  编辑  收藏 所属分类: OA(Moss+Infopath)


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

Hibernate

友情链接

搜索

最新评论

阅读排行榜

评论排行榜