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

C#通过com端口获取sap数据(sharepoint)

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

1.导入Interop.SAPFunctionsOCX.dll      Interop.SAPlogonCtrl.dll      Interop.SAPTableFactoryCtrl

2.

       com端口,缺点:占用线程,单线程
        private void getCurrentStock(object obj)
        {
            SPItemEventProperties Pro = (SPItemEventProperties)obj;
            SPListItem item = Pro.ListItem;
            SAPFunctionsOCX.SAPFunctionsClass func = VStoSAP.getSAPFunctionsClass("", "");
            if (func != null)
            {
                string MaterialNo = getSPFieldTextValue(item["Material"]);
                string Plant = getSPFieldTextValue(item["Plant"]);

                SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("ZRFC_MM_MASTER_MRB");

                SAPFunctionsOCX.IParameter IV_MATNR = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("IV_MATNR"); //Material No.
                SAPFunctionsOCX.IParameter IV_WERKS = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("IV_WERKS"); //Plant

                IV_MATNR.Value = MaterialNo;
                IV_WERKS.Value = Plant;

                ifunc.Call();

                SAPFunctionsOCX.IParameter EV_MAKTX = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("EV_MAKTX");
                SAPFunctionsOCX.IParameter EV_PRICE = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("EV_PRICE");
                SAPFunctionsOCX.IParameter EV_MEINS = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("EV_MEINS");
                SAPFunctionsOCX.IParameter EV_DSNAM = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("EV_DSNAM");

                if (EV_MAKTX != null && EV_MAKTX.Value != null)
                {
                    item["Description"] = EV_MAKTX.Value.ToString();
                }
                if (EV_PRICE != null && EV_PRICE.Value != null)
                {
                    item["Unit Price"] = Convert.ToInt32(EV_PRICE.Value);
                }
                if (EV_MEINS != null && EV_MEINS.Value != null)
                {
                    item["Unit"] = EV_MEINS.Value.ToString();
                }
                if (EV_DSNAM != null && EV_DSNAM.Value != null)
                {
                    item["Project"] = EV_DSNAM.Value.ToString();
                }

                DisableEventFiring();
                item.SystemUpdate();
                EnableEventFiring();
            }

 

        }

 

3.Added的事件中处理:

#region 方法三:com端口,缺点:占用线程,单线程
            SPItemEventProperties Pro = properties;
            //Thread mrbThread = new Thread(new ThreadStart(getCurrentStock));
           Thread mrbThread = new Thread(getCurrentStock);
            mrbThread.SetApartmentState(ApartmentState.STA);
            //mrbThread.Start();
            mrbThread.Start(properties);
            mrbThread.Join();

            #endregion

 

4.VStoSAP类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Net;
using System.Configuration;

namespace EventHandler1888
{
    public class VStoSAP
    {
        private static string getDefaultServerName()
        {
            string hostName = Dns.GetHostName();
            if (hostName.ToUpper() == "正式服务器名字")
            {
                return "PRD";
            }
            else
            {
                return "QAS";
            }
        }

        static string FindConfigStringValue(string strSrc, string strFind)
        {
            string ss = strSrc.ToUpper();
            strFind = strFind.ToUpper();
            int nStart = ss.IndexOf(strFind, 0);
            if (nStart < 0)
                return null;
            if (nStart > 0 && ss[nStart - 1] != ';')//关键字之前必须有分号或是开始位置
                return null;


            nStart = ss.IndexOf("=", nStart);

            if (nStart < 0)
                return null;

            nStart++;
            int nEnd = ss.IndexOf(";", nStart);
            if (nEnd < 0)
            {
                return strSrc.Substring(nStart, strSrc.Length - nStart);
            }
            else
            {
                return strSrc.Substring(nStart, nEnd - nStart);
            }

        }

        internal static SAPFunctionsOCX.SAPFunctionsClass getSAPFunctionsClass(string Server, string Debug)
        {
            string connStr = "";
            GetClient(ref Server, Debug, ref connStr);

            SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass();
            login.ApplicationServer = FindConfigStringValue(connStr, "Server");
            login.Client = FindConfigStringValue(connStr, "Client");
            login.Language = FindConfigStringValue(connStr, "Language");
            login.User = FindConfigStringValue(connStr, "User");
            login.Password = FindConfigStringValue(connStr, "Psw");
            login.SystemNumber = Convert.ToInt16(FindConfigStringValue(connStr, "SysNo"));
            try
            {
                SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection();
                //conn.Language = "ZH";
                //conn.CodePage = "8400";
                if (conn.Logon(0, true))
                {
                    SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();

                    func.Connection = conn;
                    return func;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }

        }

        private static void GetClient(ref string Server, string Debug, ref string connStr)
        {
            if (string.IsNullOrEmpty(Server))
            {
                Server = getDefaultServerName();
            }
            if (Server.ToUpper() == "PRD")
            {
                if (Debug == "1")
                {
                    connStr = ConfigurationManager.AppSettings["SAPPRDDebug"];
                }
                else
                {
                    connStr = ConfigurationManager.AppSettings["SAPPRD"];
                }

            }
            if (Server.ToUpper() == "QAS")
            {
                if (Debug == "1")
                {
                    connStr = ConfigurationManager.AppSettings["SAPQASDebug"];
                }
                else
                {
                    connStr = ConfigurationManager.AppSettings["SAPQAS"];
                }
            }
        }

        /// <summary>
        /// 格式化SAPTable为DT
        /// </summary>
        /// <param name="SAPTable">SAP Table</param>
        /// <returns></returns>
        internal static DataTable convetTable(SAPTableFactoryCtrl.Table SAPTable)
        {
            try
            {
                int count = SAPTable.ColumnCount;
                DataTable dt = new DataTable();
                dt.Columns.Add("Index", typeof(int));
                for (int i = 1; i <= count; i++)
                {
                    string columnName = SAPTable.get_ColumnName(i);
                    dt.Columns.Add(columnName, typeof(string));
                }
                int countRows = SAPTable.RowCount;
                for (int i = 1; i <= countRows; i++)
                {
                    DataRow dr = dt.NewRow();
                    for (int j = 1; j < dt.Columns.Count; j++)
                    {
                        dr[j] = SAPTable.get_Cell(i, SAPTable.get_ColumnName(i)).ToString();
                    }
                    dt.Rows.Add(dr);
                }
                return dt;
            }
            catch (Exception)
            {
                return null;
            }


        }
    }
}

 

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
c#调用c++带有回调函数方法的实现发布时间:2022-07-14
下一篇:
c#在一个窗口中打开另一个窗口发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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