using System; using System.Web.Services; using System.Runtime.InteropServices; using SAPFunctionsOCX; using SAPLogonCtrl; using SAPTableFactoryCtrl; using System.Threading; using System.Text; using System.IO; using System.Data; namespace WebService2Sap { /// <summary> /// Ws2Sap 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class Ws2Sap : System.Web.Services.WebService { //从ini文件中读取登入信息 [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(); //dataset c#专用,其他语言使用此webservice会造成不便 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("MATNR", Type.GetType("System.String")); table.Columns.Add("MENGE", Type.GetType("System.String")); table.Columns.Add("MAKTX", Type.GetType("System.String")); for (int a = 1; a <= ENQ.RowCount; a++) { DataRow dr = table.NewRow(); dr["MATNR"] = ENQ.get_Cell(a, "MATNR"); dr["MENGE"] = ENQ.get_Cell(a, "MENGE"); dr["MAKTX"] = ENQ.get_Cell(a, "MAKTX"); table.Rows.Add(dr); } ds.Tables.Add(table); 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() //webservice返回一个类,需要一个无参的构造函数 { this.pe = ""; this.ds_info = null; } } } }