C# webservice 連接 sap

来源:互联网 发布:身份证登记软件 编辑:程序博客网 时间:2024/06/03 23:02

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using SAPFunctionsOCX;
using SAPLogonCtrl;
using SAPTableFactoryCtrl;
using System.Threading;
using System.Text;
using System.IO;
using System.Collections.Generic;

namespace Substrate2Pda_ws
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tripod-tech.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [DllImport("kernel32")]//返回0表示失败,非0为成功
        private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);

        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);

        public String PE;
        DataSet ds = new DataSet();
        DataTable table = new DataTable();

        [WebMethod]
        public return_info Substrate2Pda(String Zmatnr)
        {  
            CreateThread(Zmatnr);
            return_info info = new return_info(PE,ds);
            return info;
        }

        public void CreateThread(string Zmatnr)
        {
            object o = Zmatnr;
            Thread s = new Thread(new ParameterizedThreadStart(Con_Sap)); //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(o);         //Start the thread
            s.Join();           //Wait until thread run OK.
        }

        public void Con_Sap(object Zmatnr)
        {
            string iniaddress = "C:\\Windows\\Ame.ini";
            SAPLogonControlClass connctl = new SAPLogonControlClass();
            connctl.Client = ReadIniData("SapClient", "Client", "", iniaddress);
            connctl.ApplicationServer = ReadIniData("SapApplicationServer", "ApplicationServer", "", iniaddress);//Application server IP
            connctl.User = ReadIniData("SapUser", "User", "", iniaddress);
            connctl.Password = ReadIniData("SapPassword", "Password", "", iniaddress);

            Connection conn = (Connection)connctl.NewConnection();
            //登陆
            if (conn.Logon(null, true))
            {


                SAPFunctionsClass functions = new SAPFunctionsClass();
                functions.Connection = conn;
                //这里就可以传入Function Name
                Function fc = (Function)functions.Add("ZMM_SUBSTRATE2PDA");
                //这里是传入值参数
                Parameter parameter1 = (Parameter)fc.get_Exports("ZMBLNR");
                string matnr = (string)Zmatnr;
                parameter1.Value = matnr;
                //调用函数,并读取数据
                fc.Call();
                Tables tables = (Tables)fc.Tables;
                Table ENQ = (Table)tables.get_Item("ITAB");
                //table.Columns.Add("CHECK", Type.GetType("System.Boolean"));
                table.Columns.Add("狀態", Type.GetType("System.String"));
                table.Columns.Add("物料", Type.GetType("System.String"));
                table.Columns.Add("數量", Type.GetType("System.String"));
                table.Columns.Add("差異量", Type.GetType("System.String"));
                table.Columns.Add("品名", Type.GetType("System.String"));
                for (int a = 1; a <= ENQ.RowCount; a++)
                {
                    DataRow dr = table.NewRow();
                   // dr["CHECK"] = false;
                    dr["狀態"] = "X";
                    dr["物料"] = ENQ.get_Cell(a, "MATNR");                   
                    dr["數量"] = ENQ.get_Cell(a, "MENGE");
                    dr["差異量"] = ENQ.get_Cell(a, "MENGE");
                    dr["品名"] = ENQ.get_Cell(a, "MAKTX");             
                    table.Rows.Add(dr);
                }

                ds.Tables.Add(table);
               
                //datatable dt=ds.tables[0];//遍历行
                foreach(DataRow dr in table.Rows)
                {  //遍历列 
                    for(int i=0;i<table.Columns.Count;i++) 
                    {
                        if (i == 2 || i == 3)
                        {
                            dr[i] = Int32.Parse(dr[i].ToString());
                        }
                    } 
                }

                PE = "SUCCESS";
                //退出登陆
                conn.Logoff();
              
            }
            else
            {
                PE = "FAIL";
            //登入失敗
            }

        }

        #region 读Ini文件

        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }

        #endregion

        [Serializable]  //序列化
        public class return_info
        {

            public string pe;
            public DataSet ds_info;
            public return_info(string pe,DataSet ds_info)
            {
                this.pe = pe;
                this.ds_info = ds_info;
            }
            public return_info()  
            {
                this.pe = "";
                this.ds_info = null;
            }

        }


    }
}