private String SapLogon(String userName, String password, out String statusCode, out String statusMessage) { String returnValue = String.Empty; statusCode = StatusCode.OK.ToString(); statusMessage = String.Empty; // 写入SAP try { String connectString = ConfigHelper.GetValue("SAPConnectionString").ToString().Replace("{Username}", userName).Replace("{Password}", password); SAPLogonCtrl.SAPLogonControlClass SapLogon = new SAPLogonCtrl.SAPLogonControlClass(); if (!ParseSapConnection(SapLogon, connectString)) { statusCode = "-2"; statusMessage = "SAP连接串格式错误。"; return(returnValue); } // 以下建立与R3的通信机制 SAPLogonCtrl.Connection EnterSap = (SAPLogonCtrl.Connection)SapLogon.NewConnection();//建立连接 if (EnterSap.Logon(0, true) == false) { statusCode = "-1"; statusMessage = "连接SAP失败。"; return(returnValue); } returnValue = userName; } // 写入SAP失败 catch (Exception exception) { this.ProcessException(exception); } return(userName); }
/// <summary> /// p.s can abstract to setting.js /// </summary> private SapConnection() { login = new SAPLogonCtrl.SAPLogonControlClass(); //login.ApplicationServer = "10.0.0.79"; //login.Client = "800"; //login.Language = "ZF"; //login.User = "******"; //login.Password = "******"; login.ApplicationServer = "10.0.0.80"; login.Client = "250"; login.Language = "ZF"; login.User = "******"; login.Password = "******"; login.SystemNumber = 00; }
/// <summary> /// 解析SAP连接字符串,赋值连接对象 /// </summary> /// <param name="SapLogon"></param> /// <param name="sapConnection"></param> /// <returns></returns> private bool ParseSapConnection(SAPLogonCtrl.SAPLogonControlClass SapLogon, String sapConnection) { try { string[] split = sapConnection.Split(new Char[] { ';', '=' }); SapLogon.Client = split[3]; SapLogon.Language = split[11]; SapLogon.User = split[7]; SapLogon.Password = split[9]; SapLogon.ApplicationServer = split[1]; SapLogon.SystemNumber = 0; return(true); } catch (Exception exception) { this.ProcessException(exception); } return(true); }
public void Main() { SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass(); login.ApplicationServer = ConfigurationManager.AppSettings["lg_ApplicationServer"]; login.Client = ConfigurationManager.AppSettings["lg_Client"]; login.Language = ConfigurationManager.AppSettings["lg_Language"]; login.User = ConfigurationManager.AppSettings["lg_User"]; login.Password = ConfigurationManager.AppSettings["lg_Password"]; login.SystemNumber = Int32.Parse(ConfigurationManager.AppSettings["lg_SystemNumber"]); SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection(); try { if (conn.Logon(0, true)) { //lblLogonStatus.Text = "登入SAP成功"; SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass(); func.Connection = conn; SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("RFC_READ_TABLE"); SAPFunctionsOCX.IParameter iQUERY_TABLE = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("QUERY_TABLE"); iQUERY_TABLE.Value = iQueryTable; SAPFunctionsOCX.IParameter iDELIMITER = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("DELIMITER"); iDELIMITER.Value = "|"; SAPTableFactoryCtrl.Tables tables = (SAPTableFactoryCtrl.Tables)ifunc.Tables; SAPTableFactoryCtrl.Table tOPTIONS = (SAPTableFactoryCtrl.Table)tables.get_Item("OPTIONS"); tOPTIONS.AppendGridData(1, 1, 1, ""); SAPTableFactoryCtrl.Table tFIELDS = (SAPTableFactoryCtrl.Table)tables.get_Item("FIELDS"); if (iQueryField != null) { int StringStart = 0; int iColumnIndex = 1; int isDoExit = 0; while (isDoExit == 0) { int StringEnd = iQueryField.ToString().IndexOf(",", StringStart); if (StringEnd == -1) { StringEnd = iQueryField.ToString().Length; isDoExit = 1; } int StringLength = StringEnd - StringStart; tFIELDS.AppendGridData(1, iColumnIndex, 1, iQueryField.ToString().Substring(StringStart, StringLength)); StringStart = StringEnd + 1; iColumnIndex = iColumnIndex + 1; } } ifunc.Call(); oException = ifunc.Exception; SAPTableFactoryCtrl.Table tDATA = (SAPTableFactoryCtrl.Table)tables.get_Item("DATA"); for (int m = 1; m <= tDATA.RowCount; m++) { int StringStart = 0; Dictionary <string, object> listObject = new Dictionary <string, object>(); for (int n = 1; n <= tFIELDS.RowCount; n++) { int StringEnd = tDATA.get_Cell(m, 1).ToString().IndexOf("|", StringStart); if (StringEnd == -1) { StringEnd = tDATA.get_Cell(m, 1).ToString().Length; } int StringLength = StringEnd - StringStart; listObject.Add(tFIELDS.get_Cell(n, 1).ToString(), tDATA.get_Cell(m, 1).ToString().Substring(StringStart, StringLength)); StringStart = StringEnd + 1; } listResult.Add(listObject); } conn.Logoff(); } else { throw new Exception("Logon Fail"); } Result = listResult; } catch (Exception ex) { throw ex; } }
public void Main() { SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass(); login.ApplicationServer = ConfigurationManager.AppSettings["lg_ApplicationServer"]; login.Client = ConfigurationManager.AppSettings["lg_Client"]; login.Language = ConfigurationManager.AppSettings["lg_Language"]; login.User = ConfigurationManager.AppSettings["lg_User"]; login.Password = ConfigurationManager.AppSettings["lg_Password"]; login.SystemNumber = Int32.Parse(ConfigurationManager.AppSettings["lg_SystemNumber"]); string str_json = string.Empty; SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection(); //conn.SAPRouter try { if (conn.Logon(0, true)) { //lblLogonStatus.Text = "登入SAP成功"; SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass(); func.Connection = conn; SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("RFC_READ_TABLE"); SAPFunctionsOCX.IParameter iQUERY_TABLE = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("QUERY_TABLE"); iQUERY_TABLE.Value = iQueryTable; SAPFunctionsOCX.IParameter iDELIMITER = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("DELIMITER"); iDELIMITER.Value = "|"; SAPTableFactoryCtrl.Tables tables = (SAPTableFactoryCtrl.Tables)ifunc.Tables; SAPTableFactoryCtrl.Table tOPTIONS = (SAPTableFactoryCtrl.Table)tables.get_Item("OPTIONS"); tOPTIONS.AppendGridData(1, 1, 1, ""); SAPTableFactoryCtrl.Table tFIELDS = (SAPTableFactoryCtrl.Table)tables.get_Item("FIELDS"); //tFIELDS.AppendGridData(1, 1, 1, "BNAME"); //tFIELDS.AppendGridData(1, 2, 1, "TERMINAL"); ifunc.Call(); SAPTableFactoryCtrl.Table tDATA = (SAPTableFactoryCtrl.Table)tables.get_Item("DATA"); //得到一個DataTable物件 DataTable dt = new DataTable(); DataTable dt2 = new DataTable(); for (int i = 1; i <= tFIELDS.RowCount; i++) { dt.Columns.Add(tFIELDS.get_Cell(i, 1).ToString()); } for (int m = 1; m <= tDATA.RowCount; m++) { DataRow dr = dt.NewRow(); int StringStart = 0; for (int n = 1; n <= tFIELDS.RowCount; n++) { int StringEnd = tDATA.get_Cell(m, 1).ToString().IndexOf("|", StringStart); if (StringEnd == -1) { StringEnd = tDATA.get_Cell(m, 1).ToString().Length; } int StringLength = StringEnd - StringStart; dr[tFIELDS.get_Cell(n, 1).ToString()] = tDATA.get_Cell(m, 1).ToString().Substring(StringStart, StringLength); StringStart = StringEnd + 1; } dt.Rows.Add(dr); } //將DataTable轉成JSON字串 str_json = JsonConvert.SerializeObject(dt, Formatting.Indented); dt2 = JsonConvert.DeserializeObject <DataTable>(str_json); conn.Logoff(); } else { throw new Exception("Logon Fail"); } Result = str_json; } catch (Exception ex) { throw ex; } }
private String SapLogon(String userName, String password, out String statusCode, out String statusMessage) { String returnValue = String.Empty; statusCode = StatusCode.OK.ToString(); statusMessage = String.Empty; // д��SAP try { String connectString = ConfigHelper.GetValue("SAPConnectionString").ToString().Replace("{Username}", userName).Replace("{Password}", password); SAPLogonCtrl.SAPLogonControlClass SapLogon = new SAPLogonCtrl.SAPLogonControlClass(); if (!ParseSapConnection(SapLogon, connectString)) { statusCode = "-2"; statusMessage = "SAP���Ӵ���ʽ����"; return returnValue; } // ���½�����R3��ͨ�Ż��� SAPLogonCtrl.Connection EnterSap = (SAPLogonCtrl.Connection)SapLogon.NewConnection();//�������� if (EnterSap.Logon(0, true) == false) { statusCode = "-1"; statusMessage = "����SAPʧ�ܡ�"; return returnValue; } returnValue = userName; } // д��SAPʧ�� catch (Exception exception) { this.ProcessException(exception); } return userName; }