在vs2005中使用Com连接SAP系统-Web方式

测试结果: VS2005 c# + SAP ECC6.0  未通过。

VB测试通过,实际应用可以采用C#与VB相结合的方式。

 

原文地址:http://www.cnblogs.com/flysky927/archive/2009/01/04/1368484.html

 

昨天讲述了以下如何在VS2005中使用form的形式来调用SAP ECC6.0中的Function Module,今天将记录一下在VS2005中以web的形式来调用Function module.

      以web形式对function module进行调用与form形式基本一样,唯一值得注意的地方就是:"An ActiveX control must be run in an STA apartment. Because the attribute STAThread is applied to the Main method of a WinForm class by default with a WinForms Project, the main thread of your app will run in STA mode.".也就是说有些AcrtiveX控件或者Com组件必须运行在单线程单元下(STA:Single  Thread  Apartment ),否则的话会抛出“Bad variant type”异常。解决方案为:新开一个线程,并将该线程的运行模式设置为STA,然后再改线程下对Com组件或者ActiveX控件进行调用。

      对应到我们的事例中,如果不使用STA模式运行,我们可以连接到SAP系统,但调用Function Module的时候会抛出“Bad variant type”异常。所以要讲调用Function Module的代码在新开的线程中执行。具体步骤如下:

      一,添加对Interop.SAPFunctionsOCX.dll以及Interop.SAPLogonCtrl.dll和Interop.SAPTableFactoryCtrl.dllcom组件的引用。

      二,新开一个线程,并将该线程的运行模式设置为STA. 并将登录SAP系统以及调用Function module的方法运行在该线程下!代码如下:

    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread s = new System.Threading.Thread(new System.Threading.ThreadStart(test)); //Create a new thread and set the method test() run in this thread
        s.SetApartmentState(System.Threading.ApartmentState.STA);                                        //Set the run mode 'STA'
        s.Start();                                                                                       //Start the thread
        s.Join();                                                                                        //Wait until thread run OK.
        GridView1.DataSource = dt;
        GridView1.DataBind();
        msg.Text = "Get Data from 'ENQUEUE_READ' OK!";
    }

    private void test()
    {
        SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass();
        login.ApplicationServer = "";
        login.Client = "";
        login.Language = "EN";
        login.User = username.Text;
        login.Password = Psw.Text;
        login.SystemNumber = 00;
        SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection();

        if (conn.Logon(0, true))
        {
            SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();
            func.Connection = conn;
            SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("ENQUEUE_READ");
            SAPFunctionsOCX.IParameter gclient = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("GCLIENT");
            gclient.Value = "301";
            SAPFunctionsOCX.IParameter GUNAME = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("GUNAME");
            GUNAME.Value = "";
            SAPFunctionsOCX.IParameter LOCAL = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("LOCAL");
            LOCAL.Value = "0";
            ifunc.Call();
            SAPTableFactoryCtrl.Tables tables = (SAPTableFactoryCtrl.Tables)ifunc.Tables;
            SAPTableFactoryCtrl.Table ENQ = (SAPTableFactoryCtrl.Table)tables.get_Item("ENQ");
            int n = ENQ.RowCount;
            dt = GetTable();
            for (int i = 1; i <= n; i++)
            {
                DataRow dr = dt.NewRow();
                dr["GNAME"] = ENQ.get_Cell(i, "GNAME").ToString();
                dr["GUNAME"] = ENQ.get_Cell(i, "GUNAME").ToString();
                dr["GARG"] = ENQ.get_Cell(i, "GARG").ToString();
                dr["GOBJ"] = ENQ.get_Cell(i, "GOBJ").ToString();
                dr["GTDATE"] = ENQ.get_Cell(i, "GTDATE").ToString();
                dt.Rows.Add(dr);
            }
        }
    }

通过以上的设置就可以在Web中调用Function Module了。此方式已经试验成功。园子里面另外一位仁兄弟到了另外一种方法http://www.cnblogs.com/jirigala/archive/2008/11/26/1341345.html 但是该方法我没有试验成功,不知道是我哪设置出问题了还是怎么回事,如果有知情者请告知,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值