public RfcParameterMetadata_ForUI Convert_To_RfcParameterMetadata_ForUI(RfcParameterMetadata metadata) { RfcParameterMetadata_ForUI item = new RfcParameterMetadata_ForUI(); item.Direction = metadata.Direction; item.DataType = metadata.DataType; item.Decimals = metadata.Decimals; item.DefaultValue = metadata.DefaultValue; item.Documentation = metadata.Documentation; item.Name = metadata.Name; item.NucLength = metadata.NucLength; item.Optional = metadata.Optional; item.UcLength = metadata.UcLength; return item; }
public RfcParameterMetadata_ForUI Convert_To_RfcParameterMetadata_ForUI(RfcParameterMetadata metadata) { RfcParameterMetadata_ForUI item = new RfcParameterMetadata_ForUI(); item.Direction = metadata.Direction; item.DataType = metadata.DataType; item.Decimals = metadata.Decimals; item.DefaultValue = metadata.DefaultValue; item.Documentation = metadata.Documentation; item.Name = metadata.Name; item.NucLength = metadata.NucLength; item.Optional = metadata.Optional; item.UcLength = metadata.UcLength; return(item); }
/// <summary> /// 把RF函数中的字段元数据添加到Table中 /// </summary> /// <param name="pMetadata"></param> /// <param name="dt"></param> private static void AddMetadataToTable(ref RfcParameterMetadata pMetadata, ref DataTable dt) { DataRow dtrow = dt.NewRow(); dtrow["Name"] = pMetadata.Name; dtrow["DataType"] = pMetadata.DataType; dtrow["Decimals"] = pMetadata.Decimals; if (pMetadata.DefaultValue != null) { if (pMetadata.DefaultValue.StartsWith("'")) { pMetadata.DefaultValue = pMetadata.DefaultValue.Remove(0, 1); } if (pMetadata.DefaultValue.EndsWith("'")) { pMetadata.DefaultValue = pMetadata.DefaultValue.Remove(pMetadata.DefaultValue.Length - 1, 1); } if (pMetadata.DefaultValue.ToUpper() == "SPACE") { pMetadata.DefaultValue = string.Empty; } dtrow["DefaultValue"] = pMetadata.DefaultValue; } dtrow["Length"] = pMetadata.NucLength; dtrow["Optional"] = pMetadata.Optional; dtrow["Documentation"] = pMetadata.Documentation; if (pMetadata.DataType == RfcDataType.STRUCTURE) { RfcContainerMetadata <RfcFieldMetadata> meta = pMetadata.ValueMetadataAsStructureMetadata; dtrow["DataTypeName"] = meta.Name; } else if (pMetadata.DataType == RfcDataType.TABLE) { RfcContainerMetadata <RfcFieldMetadata> meta = pMetadata.ValueMetadataAsTableMetadata; RfcTableMetadata tablem = pMetadata.ValueMetadataAsTableMetadata; dtrow["DataTypeName"] = tablem.LineType.Name; } else { } dt.Rows.Add(dtrow); }
/// <summary> /// 根据参数创建内表 /// </summary> /// <param name="rfcpm"></param> /// <param name="ceng"></param> /// <param name="tname"></param> /// <returns></returns> public DataTable createStructureByParameter(RfcParameterMetadata rfcpm, string ceng, string tname) { DataTable dt = new DataTable(); dt.Namespace = ceng; dt.TableName = ceng + "." + tname; if (rfcpm.DataType == RfcDataType.STRUCTURE) { for (int j = 0; j < rfcpm.ValueMetadataAsStructureMetadata.FieldCount; j++) { DataColumn dc = new DataColumn(); if (rfcpm.ValueMetadataAsStructureMetadata[j].DataType == RfcDataType.TABLE) { dc.Namespace = "TABLE"; dc.ColumnName = rfcpm.ValueMetadataAsStructureMetadata[j].Name; dc.Caption = rfcpm.ValueMetadataAsStructureMetadata[j].Documentation; dt.Columns.Add(dc); createStructureByField(rfcpm.ValueMetadataAsStructureMetadata[j], dt.TableName); } else if (rfcpm.ValueMetadataAsStructureMetadata[j].DataType == RfcDataType.STRUCTURE) { dc.Namespace = "STRUCTURE"; dc.ColumnName = rfcpm.ValueMetadataAsStructureMetadata[j].Name; dc.Caption = rfcpm.ValueMetadataAsStructureMetadata[j].Documentation; dt.Columns.Add(dc); createStructureByField(rfcpm.ValueMetadataAsStructureMetadata[j], dt.TableName); } else { dc.Namespace = "STRING"; dc.ColumnName = rfcpm.ValueMetadataAsStructureMetadata[j].Name; dc.Caption = rfcpm.ValueMetadataAsStructureMetadata[j].Documentation; dt.Columns.Add(dc); } } } else if (rfcpm.DataType == RfcDataType.TABLE) { for (int j = 0; j < rfcpm.ValueMetadataAsTableMetadata.LineType.FieldCount; j++) { DataColumn dc = new DataColumn(); if (rfcpm.ValueMetadataAsTableMetadata[j].DataType == RfcDataType.TABLE) { dc.Namespace = "TABLE"; dc.ColumnName = rfcpm.ValueMetadataAsTableMetadata[j].Name; dc.Caption = rfcpm.ValueMetadataAsTableMetadata[j].Documentation; dt.Columns.Add(dc); createStructureByField(rfcpm.ValueMetadataAsTableMetadata[j], dt.TableName); } else if (rfcpm.ValueMetadataAsTableMetadata[j].DataType == RfcDataType.STRUCTURE) { dc.Namespace = "STRUCTURE"; dc.ColumnName = rfcpm.ValueMetadataAsTableMetadata[j].Name; dc.Caption = rfcpm.ValueMetadataAsTableMetadata[j].Documentation; dt.Columns.Add(dc); createStructureByField(rfcpm.ValueMetadataAsTableMetadata[j], dt.TableName); } else { dc.Namespace = "STRING"; dc.ColumnName = rfcpm.ValueMetadataAsTableMetadata[j].Name; dc.Caption = rfcpm.ValueMetadataAsTableMetadata[j].Documentation; dt.Columns.Add(dc); } } } else { } dsRFC.Tables.Add(dt); return(dt); }
/// <summary> /// 读取RFCFunction的所有元数据,并以DataTable的形式输出 /// </summary> /// <param name="sysName"></param> /// <param name="funame"></param> /// <returns></returns> public static FunctionMetaAsDataTable GetRfcFuncMetaAsDataTable(string sysName, string funame) { FunctionMetaAsDataTable metaTable = null; try { RfcFunctionMetadata MetaData = GetRfcFunctionMetadata(sysName, funame); metaTable = new FunctionMetaAsDataTable(); DataTable dtImport = FunctionMetaAsDataTable.ParameterDefinitionView(); DataTable dtExport = dtImport.Copy(); DataTable dtChanging = dtImport.Copy(); DataTable dtTables = dtImport.Copy(); //根据参数的方向,分为四种(CHANGING,EXPORT,IMPORT,TABLES); for (int i = 0; i < MetaData.ParameterCount; i++) { RfcParameterMetadata pMetadata = MetaData[i]; switch (pMetadata.Direction) { //创建四个方向的参数列表 case RfcDirection.CHANGING: AddMetadataToTable(ref pMetadata, ref dtChanging); break; case RfcDirection.EXPORT: AddMetadataToTable(ref pMetadata, ref dtExport); break; case RfcDirection.IMPORT: AddMetadataToTable(ref pMetadata, ref dtImport); break; case RfcDirection.TABLES: AddMetadataToTable(ref pMetadata, ref dtTables); break; } if (pMetadata.DataType == RfcDataType.STRUCTURE) { RfcStructureMetadata strucmeta = pMetadata.ValueMetadataAsStructureMetadata; RfcContainerMetadata <RfcFieldMetadata> meta = pMetadata.ValueMetadataAsStructureMetadata; if (!metaTable.StructureDetail.Keys.Contains(meta.Name)) { //在这里创建结构体的定义表格 DataTable dtStructure = FunctionMetaAsDataTable.StructDefinitionView(); for (int f = 0; f < strucmeta.FieldCount; f++) { DataRow dr = dtStructure.NewRow(); RfcFieldMetadata fieldm = strucmeta[f]; dr["Name"] = fieldm.Name; dr["DataType"] = fieldm.DataType.ToString(); dr["Decimals"] = fieldm.Decimals; dr["Length"] = fieldm.NucLength; dr["Documentation"] = fieldm.Documentation; dtStructure.Rows.Add(dr); } metaTable.StructureDetail.Add(meta.Name, dtStructure); } if (!metaTable.InputTable.Keys.Contains(pMetadata.Name)) { //用于输入数据的DataTable; DataTable dtInput = new DataTable(); for (int f = 0; f < strucmeta.FieldCount; f++) { RfcFieldMetadata fieldm = strucmeta[f]; DataColumn dc = null; String colName = fieldm.Name; dc = new DataColumn(colName, Type.GetType("System.String")); dc.MaxLength = fieldm.NucLength; dc.Caption = fieldm.Documentation; dtInput.Columns.Add(dc); } metaTable.InputTable.Add(pMetadata.Name, dtInput); } } else if (pMetadata.DataType == RfcDataType.TABLE) { //在这里创建表的定义明细表格 RfcTableMetadata tablem = pMetadata.ValueMetadataAsTableMetadata; if (!metaTable.StructureDetail.Keys.Contains(tablem.LineType.Name)) { DataTable dtTable = FunctionMetaAsDataTable.StructDefinitionView(); for (int f = 0; f < tablem.LineType.FieldCount; f++) { DataRow dr = dtTable.NewRow(); RfcFieldMetadata fieldm = tablem.LineType[f]; dr["Name"] = fieldm.Name; dr["DataType"] = fieldm.DataType; dr["Decimals"] = fieldm.Decimals; dr["Length"] = fieldm.NucLength; dr["Documentation"] = fieldm.Documentation; dtTable.Rows.Add(dr); } metaTable.StructureDetail.Add(tablem.LineType.Name, dtTable); } if (!metaTable.InputTable.Keys.Contains(pMetadata.Name)) { //用于输入数据的DataTable; DataTable dtInput = new DataTable(); for (int f = 0; f < tablem.LineType.FieldCount; f++) { RfcFieldMetadata fieldm = tablem.LineType[f]; DataColumn dc = null; String colName = fieldm.Name; dc = new DataColumn(colName, Type.GetType("System.String")); dc.MaxLength = fieldm.NucLength; dc.Caption = fieldm.Documentation; dtInput.Columns.Add(dc); } metaTable.InputTable.Add(pMetadata.Name, dtInput); } } } metaTable.Import = dtImport; metaTable.Export = dtExport; metaTable.Changing = dtChanging; metaTable.Tables = dtTables; } catch (Exception ee) { metaTable = null; throw new SAPException(ee.Message); } return(metaTable); }
/// <summary> /// 读取RFC函数的全部信息 /// </summary> /// <param name="sysName"></param> /// <param name="funame"></param> /// <returns></returns> public static RfcFunctionMetaAsList GetFuncMetaAsList(string sysName, string funame) { funame = funame.ToUpper().Trim(); RfcFunctionMetadata MetaData = GetRfcFunctionMetadata(sysName, funame); RfcFunctionMetaAsList metaList = new RfcFunctionMetaAsList(); // metaList.Import.Add( //根据参数的方向,分为四种(CHANGING,EXPORT,IMPORT,TABLES); for (int i = 0; i < MetaData.ParameterCount; i++) { RfcParameterMetadata pMetadata = MetaData[i]; FunctionField field = null; String dataTypeName = String.Empty; if (pMetadata.DataType == RfcDataType.STRUCTURE) { RfcContainerMetadata <RfcFieldMetadata> meta = pMetadata.ValueMetadataAsStructureMetadata; dataTypeName = meta.Name; } else if (pMetadata.DataType == RfcDataType.TABLE) { RfcContainerMetadata <RfcFieldMetadata> meta = pMetadata.ValueMetadataAsTableMetadata; RfcTableMetadata tablem = pMetadata.ValueMetadataAsTableMetadata; dataTypeName = tablem.LineType.Name; } field = new FunctionField(pMetadata.Name, pMetadata.Direction.ToString(), pMetadata.DataType.ToString(), pMetadata.Decimals, pMetadata.DefaultValue, pMetadata.NucLength, pMetadata.Optional, pMetadata.Documentation, dataTypeName); switch (pMetadata.Direction) { //创建四个方向的参数列表 case RfcDirection.CHANGING: metaList.Changing.Add(pMetadata.Name, field); break; case RfcDirection.EXPORT: metaList.Export.Add(pMetadata.Name, field); break; case RfcDirection.IMPORT: metaList.Import.Add(pMetadata.Name, field); break; case RfcDirection.TABLES: metaList.Tables.Add(pMetadata.Name, field); break; } if (pMetadata.DataType == RfcDataType.STRUCTURE) { RfcContainerMetadata <RfcFieldMetadata> fieldMeta = pMetadata.ValueMetadataAsStructureMetadata; if (!metaList.StructureDetail.Keys.Contains(fieldMeta.Name)) { RfcStructureMetadata strucmeta = pMetadata.ValueMetadataAsStructureMetadata; List <StructureField> fieldList = new List <StructureField>(); for (int f = 0; f < strucmeta.FieldCount; f++) { RfcFieldMetadata fieldm = strucmeta[f]; StructureField structureField = new StructureField(fieldm.Name, fieldm.DataType.ToString(), fieldm.Decimals, fieldm.NucLength, fieldm.Documentation); fieldList.Add(structureField); } metaList.StructureDetail.Add(fieldMeta.Name, fieldList); } } else if (pMetadata.DataType == RfcDataType.TABLE) { RfcTableMetadata tableMeta = pMetadata.ValueMetadataAsTableMetadata; if (!metaList.StructureDetail.Keys.Contains(tableMeta.LineType.Name)) { List <StructureField> tableFieldList = new List <StructureField>(); for (int f = 0; f < tableMeta.LineType.FieldCount; f++) { RfcFieldMetadata fieldm = tableMeta.LineType[f]; StructureField structureField = new StructureField(fieldm.Name, fieldm.DataType.ToString(), fieldm.Decimals, fieldm.NucLength, fieldm.Documentation); tableFieldList.Add(structureField); } metaList.StructureDetail.Add(tableMeta.LineType.Name, tableFieldList); } } } return(metaList); }
/// <summary> /// 把RF函数中的字段元数据添加到Table中 /// </summary> /// <param name="pMetadata"></param> /// <param name="dt"></param> private static void AddMetadataToTable(ref RfcParameterMetadata pMetadata, ref DataTable dt) { DataRow dtrow = dt.NewRow(); dtrow["Name"] = pMetadata.Name; dtrow["DataType"] = pMetadata.DataType; dtrow["Decimals"] = pMetadata.Decimals; if (pMetadata.DefaultValue != null) { if (pMetadata.DefaultValue.StartsWith("'")) { pMetadata.DefaultValue = pMetadata.DefaultValue.Remove(0, 1); } if (pMetadata.DefaultValue.EndsWith("'")) { pMetadata.DefaultValue = pMetadata.DefaultValue.Remove(pMetadata.DefaultValue.Length - 1, 1); } if (pMetadata.DefaultValue.ToUpper() == "SPACE") { pMetadata.DefaultValue = string.Empty; } dtrow["DefaultValue"] = pMetadata.DefaultValue; } dtrow["Length"] = pMetadata.NucLength; dtrow["Optional"] = pMetadata.Optional; dtrow["Documentation"] = pMetadata.Documentation; if (pMetadata.DataType == RfcDataType.STRUCTURE) { RfcContainerMetadata<RfcFieldMetadata> meta = pMetadata.ValueMetadataAsStructureMetadata; dtrow["DataTypeName"] = meta.Name; } else if (pMetadata.DataType == RfcDataType.TABLE) { RfcContainerMetadata<RfcFieldMetadata> meta = pMetadata.ValueMetadataAsTableMetadata; RfcTableMetadata tablem = pMetadata.ValueMetadataAsTableMetadata; dtrow["DataTypeName"] = tablem.LineType.Name; } else { } dt.Rows.Add(dtrow); }
/// <summary> /// 动态调用RFC函数 /// </summary> /// <param name="sysName"></param> /// <param name="funame"></param> /// <param name="input"></param> /// <param name="output"></param> /// <returns></returns> public static bool InvokeFunction(string sysName, string funame, MetaValueList input, out MetaValueList output) { try { RfcFunctionMetadata MetaData = SAPFunctionMeta.GetRfcFunctionMetadata(sysName, funame); IRfcFunction function = MetaData.CreateFunction(); //初步序列化后的参数还需要进一步进行格式化,把结构体与表格都转化成SAP格式。 if (input != null) { //填充所有的参数 List <String> KeyIndex = new List <string>(); KeyIndex.AddRange(input.FieldValueList.Keys); KeyIndex.AddRange(input.StructureValueList.Keys); KeyIndex.AddRange(input.TableValueList.Keys); KeyIndex.ForEach(x => x.ToUpper().Trim()); for (int i = 0; i < MetaData.ParameterCount; i++) { RfcParameterMetadata pMetadata = MetaData[i]; if (pMetadata.Direction == RfcDirection.EXPORT) { continue; } if (!KeyIndex.Contains(pMetadata.Name)) { continue; } // Dictionary<String, Object> ParameterList = new Dictionary<string, object>(); if (pMetadata.DataType == RfcDataType.STRUCTURE) { if (input.StructureValueList.ContainsKey(pMetadata.Name)) { Dictionary <String, String> structure; input.StructureValueList.TryGetValue(pMetadata.Name, out structure); IRfcStructure str = function.GetStructure(pMetadata.Name, true); if (structure != null) { for (int s = 0; s < str.Metadata.FieldCount; s++) { RfcFieldMetadata field = str.Metadata[s]; if (structure.Keys.Contains(field.Name)) { Object o = Converts.ObjectToRfcValue(structure[field.Name], field.DataType); str.SetValue(field.Name, o); } } } } } else if (pMetadata.DataType == RfcDataType.TABLE) { List <Dictionary <String, String> > tablelist; input.TableValueList.TryGetValue(pMetadata.Name, out tablelist); if (tablelist != null) { IRfcTable table = function.GetTable(pMetadata.Name); for (int j = 0; j < tablelist.Count; j++) { table.Append(); for (int k = 0; k < table.Metadata.LineType.FieldCount; k++) { RfcFieldMetadata field = table.Metadata.LineType[k]; if (tablelist[j].Keys.Contains(field.Name)) { Object o = Converts.ObjectToRfcValue(tablelist[j][field.Name], field.DataType); table.SetValue(field.Name, o); } } } } } else { String value = ""; input.FieldValueList.TryGetValue(pMetadata.Name, out value); Object o = Converts.ObjectToRfcValue(value, pMetadata.DataType); function.SetValue(pMetadata.Name, o); } } } MetaValueList outputlist = new MetaValueList(); try { RfcDestination destination = SAPDestination.GetDesByName(sysName); function.Invoke(destination); } catch (RfcAbapException ee) { throw new SAPException(ee.Key + ee.Message + ee.PlainText); } catch (RfcAbapRuntimeException runtimeEx) { throw new SAPException(runtimeEx.Key + runtimeEx.Message + runtimeEx.PlainText); } for (int i = 0; i < MetaData.ParameterCount; i++) { RfcParameterMetadata pMetadata = MetaData[i]; if (pMetadata.Direction == RfcDirection.IMPORT) { continue; } // outputlist.FieldTypeList.Add(pMetadata.Name, pMetadata.DataType.ToString()); if (pMetadata.DataType == RfcDataType.STRUCTURE) { Dictionary <String, String> stru = null; IRfcStructure structure = function.GetStructure(pMetadata.Name, false); if (structure != null) { stru = new Dictionary <string, string>(); for (int s = 0; s < structure.Metadata.FieldCount; s++) { RfcFieldMetadata field = structure.Metadata[s]; Object result = null; if (field.DataType == RfcDataType.BYTE) { result = structure.GetString(field.Name); } else { result = structure.GetValue(field.Name); } result = Converts.RfcToDoNetValue(result, field.DataType).ToString(); stru.Add(field.Name, result.ToString()); } if (stru != null) { outputlist.StructureValueList.Add(pMetadata.Name, stru); } } } else if (pMetadata.DataType == RfcDataType.TABLE) { List <Dictionary <String, String> > outTableList = null; IRfcTable outTable = function.GetTable(pMetadata.Name, false); if (outTable != null) { outTableList = new List <Dictionary <string, string> >(); for (int s = 0; s < outTable.RowCount; s++) { IRfcStructure rfcTableLine = outTable[s]; Dictionary <String, String> row = new Dictionary <string, string>(); for (int z = 0; z < rfcTableLine.Metadata.FieldCount; z++) { RfcFieldMetadata field = rfcTableLine[z].Metadata; Object result = null; if (field.DataType == RfcDataType.BYTE) { result = rfcTableLine.GetString(field.Name); } else { result = rfcTableLine.GetValue(field.Name); } result = Converts.RfcToDoNetValue(result, field.DataType).ToString(); row.Add(field.Name, result.ToString()); } outTableList.Add(row); } } outputlist.TableValueList.Add(pMetadata.Name, outTableList); } else { Object result = function.GetValue(pMetadata.Name); result = Converts.RfcToDoNetValue(result, pMetadata.DataType); outputlist.FieldValueList.Add(pMetadata.Name, result.ToString()); } } output = outputlist; return(true); } catch (Exception e) { output = null; throw new SAPException(e.Message); } }
private void btnRFCTest_Click(object sender, EventArgs e) { try { FileHelper.SaveRFCName(txtRFCName.Text); string rfcName = txtRFCName.Text; SAPDestinationConfiguration sapDestinationConfiguration = new SAPDestinationConfiguration(); IRfcFunction rfcFunction = null; try { // ishak.kulekci 24.06.2013 // Destination configuration already initialized hatasını engelleme, bunun için önce TryGetDestination kullanılır RfcDestination rfcDestination = RfcDestinationManager.TryGetDestination("SAPSYSTEM"); if (rfcDestination == null) { try { RfcDestinationManager.RegisterDestinationConfiguration(sapDestinationConfiguration); } catch (Exception ex) { } rfcDestination = RfcDestinationManager.GetDestination("SAPSYSTEM"); } //RfcDestination rfcDestination = RfcDestinationManager.GetDestination("SAPSYSTEM"); //rfcFunction = rfcDestination.Repository.CreateFunction("Z_RFC_YK_BEKPRIM_TO_INTERNET"); ////rfcFunction.SetValue("KUNNR", "100092"); // aygaz //rfcFunction.SetValue("KUNNR", musteriNumarasi); ////rfcFunction.SetValue("DATAB", "20130601"); //rfcFunction.SetValue("DATAB", baslangicTarihi.Value.ToString("yyyyMMdd", CultureInfo.GetCultureInfo("tr-tr"))); ////rfcFunction.SetValue("DATBI", "20130605"); //rfcFunction.SetValue("DATBI", bitisTarihi.Value.ToString("yyyyMMdd", CultureInfo.GetCultureInfo("tr-tr"))); //rfcFunction.Invoke(rfcDestination); rfcFunction = rfcDestination.Repository.CreateFunction(rfcName); //rfcFunction.Metadata //RfcParameterMetadata describing an importing/exporting/tables parameter of a function module List <RfcParameterMetadata_ForUI> listRfcParameterMetadataWhichDirectionImport = new List <RfcParameterMetadata_ForUI>(); //List<RfcParameterMetadata_ForUI> listRfcParameterMetadataWhichDirectionTables = new List<RfcParameterMetadata_ForUI>(); //List<DataTable> listDataTableToImport = new List<DataTable>(); List <RFCTableWhichHasDataTableAndFieldInfo> listRFCTable = new List <RFCTableWhichHasDataTableAndFieldInfo>(); int parameterCount = rfcFunction.Metadata.ParameterCount; for (int i = 0; i < parameterCount; i++) { RfcParameterMetadata rfcParameterMetadata = rfcFunction.Metadata[i]; //rfcParameterMetadata.DefaultValue //rfcParameterMetadata.Direction //RfcDirection.CHANGING //RfcDirection.EXPORT //RfcDirection.IMPORT //RfcDirection.TABLES //rfcParameterMetadata.Optional //rfcParameterMetadata.Name if (rfcParameterMetadata.Direction == RfcDirection.IMPORT) { listRfcParameterMetadataWhichDirectionImport.Add(Convert_To_RfcParameterMetadata_ForUI(rfcParameterMetadata)); } else if (rfcParameterMetadata.Direction == RfcDirection.TABLES) { RfcTableMetadata rfcTableMetadata = rfcParameterMetadata.ValueMetadataAsTableMetadata; int fieldCount = rfcTableMetadata.LineType.FieldCount; // field adeti kadar Datatable nesnesi oluşturup kolon eklemesi yapılacak DataTable dt = new DataTable(rfcTableMetadata.Name); List <RfcFieldMetadata_ForUI> listRfcFieldMetadata = new List <RfcFieldMetadata_ForUI>(); for (int j = 0; j < fieldCount; j++) { RfcFieldMetadata rfcFieldMetadata = rfcTableMetadata[j]; DataColumn dc = new DataColumn(rfcFieldMetadata.Name); dt.Columns.Add(dc); listRfcFieldMetadata.Add(Convert_To_RfcFieldMetadata_ForUI(rfcFieldMetadata)); } //listDataTableToImport.Add(dt); //listRfcParameterMetadataWhichDirectionTables.Add(Convert_To_RfcParameterMetadata_ForUI(rfcParameterMetadata)); RFCTableWhichHasDataTableAndFieldInfo rfcTable = new RFCTableWhichHasDataTableAndFieldInfo(); rfcTable.TableParameterMetadata = Convert_To_RfcParameterMetadata_ForUI(rfcParameterMetadata); rfcTable.DataTableForItems = dt; rfcTable.ListForFieldMetadata = listRfcFieldMetadata; listRFCTable.Add(rfcTable); } } gvImportParams.AutoGenerateColumns = false; gvImportParams.DataSource = listRfcParameterMetadataWhichDirectionImport; // tablolar elde edilip, tabcontrol içine tabpage olarak eklenir // her tabpage içinde datagridview eklenir, kolon değerlerini alma sağlanır foreach (RFCTableWhichHasDataTableAndFieldInfo itemTable in listRFCTable) { TabPage page = new TabPage(); page.Text = itemTable.TableParameterMetadata.Name; page.Name = "tp" + itemTable.TableParameterMetadata.Name; page.Width = 550; page.Height = 300; DataGridView gv = new DataGridView(); gv.Name = "gv" + itemTable.TableParameterMetadata.Name; // grid kaynağı bağlanmalı gv.DataSource = itemTable.DataTableForItems; gv.Anchor = AnchorStyles.Left | AnchorStyles.Right; gv.Location = new Point(5, 35); gv.Margin = new Padding(3); gv.Width = 540; gv.Height = 200; page.Controls.Add(gv); tcTablesToImport.TabPages.Add(page); } } catch (Exception exception) { throw exception; } finally { try { RfcDestinationManager.UnregisterDestinationConfiguration(sapDestinationConfiguration); } catch (Exception ex) { } } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 填充所有的参数调用SAP RFC函数后,填充所有的参数 /// </summary> /// <param name="funame">函数名</param> /// <param name="list">输入参数列表</param> /// <param name="olist">输出参数列表</param> /// <returns></returns> private static bool InvokeFunctionFromJson(string sysName, string funame, RfcInputListJson list, out RfcOutputListJson olist) { try { if (funame == "" || null == list) { olist = null; return(false); } if (!SAPFunction.CheckFunction(sysName, funame)) { olist = null; return(false); } RfcDestination destination = SAPDestination.GetDesByName(sysName); RfcFunctionMetadata MetaData = destination.Repository.GetFunctionMetadata(funame); IRfcFunction function = MetaData.CreateFunction(); //初步序列化后的参数还需要进一步进行格式化,把结构体与表格都转化成SAP格式。 list.All.Clear(); list.All.AddRange(list.Import); list.All.AddRange(list.Change); list.All.AddRange(list.Tables); foreach (var item in list.All) { if (item.Value == null) { continue; } RfcParameterMetadata p = MetaData[item.Name]; if (p == null) { continue; } //尝试把OBJECT反解析成对应的类型 if (p.DataType == RfcDataType.STRUCTURE) { Console.WriteLine(item.Value.GetType().ToString()); if (item.Value.GetType().ToString() != "Newtonsoft.Json.Linq.JArray" && item.Value.GetType().ToString() != "System.Array") { continue; } if (item.Value.GetType().ToString() != "System.Array") { // continue; } IRfcStructure str = function.GetStructure(item.Name, true); var arr = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(item.Value.ToString()); if (arr.Count == 1) { //结构使用第一行 var o = arr[0]; for (int s = 0; s < str.Metadata.FieldCount; s++) { RfcFieldMetadata field = str.Metadata[s]; str.SetValue(field.Name, o[field.Name]); } } item.Value = str; } else if (p.DataType == RfcDataType.TABLE) { if (string.IsNullOrEmpty(item.Value.ToString())) { continue; } IRfcTable tbl = function.GetTable(item.Name, true); var arr = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(item.Value.ToString()); for (int x = 0; x < arr.Count; x++) { IRfcStructure str = tbl.Metadata.LineType.CreateStructure(); for (int s = 0; s < tbl.Metadata.LineType.FieldCount; s++) { RfcFieldMetadata field = tbl.Metadata.LineType[s]; str.SetValue(field.Name, arr[x][field.Name]); } tbl.Append(str); } item.Value = tbl; } } //填充所有的参数 for (int i = 0; i < MetaData.ParameterCount; i++) { RfcParameterMetadata pMetadata = MetaData[i]; if (list.All.Exists(x => x.Name == pMetadata.Name)) { var value = list.All.Find(x => x.Name == pMetadata.Name).Value; if (value != null) { function.SetValue(pMetadata.Name, value); } } } //远程调用函数 try { function.Invoke(destination); } catch (RfcAbapException ee) { throw new Exception(ee.Key + ee.Message); } //保留调用结果。 RfcOutputListJson outlist = new RfcOutputListJson(); //循环读取结果。把所以的结果都保存到List<object>中。 for (int i = 0; i < MetaData.ParameterCount; i++) { RfcParameterMetadata pMetadata = MetaData[i]; // if (pMetadata.Direction == RfcDirection.IMPORT) { continue; } RfcKeyValueJson d = new RfcKeyValueJson(); d.Name = pMetadata.Name; if (pMetadata.DataType == RfcDataType.STRUCTURE) { //注意,在这里就算是结构体,也把放到List中,这样可以序列化成数组格式。 List <object> tb_list = new List <object>(); IRfcStructure row = function.GetStructure(pMetadata.Name); Dictionary <string, object> rowd = new Dictionary <string, object>(); for (int x = 0; x < row.Metadata.FieldCount; x++) { rowd.Add(row[x].Metadata.Name, row[x].GetValue()); } tb_list.Add(rowd); d.Value = tb_list; } else if (pMetadata.DataType == RfcDataType.TABLE) { List <object> tb_list = new List <object>(); IRfcTable table = function.GetTable(pMetadata.Name); var readItems = table.RowCount; if (readItems > 0) { try { //保存结果到数据库。 RfcTableMetadata tableMeta = pMetadata.ValueMetadataAsTableMetadata; var _table = new SapTable(sysName, funame + "_" + pMetadata.Name, tableMeta.LineType.Name); _table.DbConnectionString = ConfigFileTool.SAPGlobalSettings.GetDefaultDbConnection(); _table.NewTable = true; _table.SaveDataTable(SAPFunction.RfcTableToDataTable(table)); // RfcTableToDb dbhelper = new RfcTableToDb(funame, d.Name, tble); // dbhelper.saveTable(); } catch (Exception ee) { throw new Exception(ee.Message); } // ThreadStart threadStart = new ThreadStart(dbhelper.saveTable); // Thread thread = new Thread(threadStart); // thread.Start(); //DbHelper.saveRfcTable(funame,ref tble); } //控制100条,数据过多会序列华出错。以后再做改善, if (readItems > 10) { readItems = 10; } for (int rowc = 0; rowc < readItems; rowc++) { IRfcStructure row = table[rowc]; Dictionary <string, object> rowd = new Dictionary <string, object>(); for (int x = 0; x < row.Metadata.FieldCount; x++) { rowd.Add(row[x].Metadata.Name, row[x].GetValue()); } tb_list.Add(rowd); } d.Value = tb_list; } else { d.Value = function.GetValue(pMetadata.Name); } //存放于不同的集合也是为了序列化方便。 switch (pMetadata.Direction) { case RfcDirection.CHANGING: outlist.Change.Add(d); break; case RfcDirection.EXPORT: outlist.Export.Add(d); break; case RfcDirection.IMPORT: outlist.Import.Add(d); break; case RfcDirection.TABLES: outlist.Tables.Add(d); break; } } olist = outlist; return(true); } catch (Exception e) { throw new SAPException(e.Message); } }
public static RfcOutputListJson GetFunMetaList(string sysName, string funame) { RfcOutputListJson paralist = new RfcOutputListJson(); try { if (string.IsNullOrEmpty(funame)) { throw new SAPException("请输入函数!!"); } if (!SAPFunction.CheckFunction(sysName, funame)) { throw new SAPException("函数不存在!!"); } funame = funame.ToUpper(); RfcDestination destination = SAPDestination.GetDesByName(sysName); destination.Repository.ClearAllMetadata(); RfcFunctionMetadata MetaData = destination.Repository.GetFunctionMetadata(funame); IRfcFunction function = null; function = MetaData.CreateFunction(); //根据参数的方向,分为四种(CHANGING,EXPORT,IMPORT,TABLES); for (int i = 0; i < MetaData.ParameterCount; i++) { RfcParameterMetadata pMetadata = MetaData[i]; paralist.All.Add(pMetadata); switch (pMetadata.Direction) { case RfcDirection.CHANGING: paralist.Change.Add(pMetadata); break; case RfcDirection.EXPORT: paralist.Export.Add(pMetadata); break; case RfcDirection.IMPORT: paralist.Import.Add(pMetadata); break; case RfcDirection.TABLES: paralist.Tables.Add(pMetadata); break; } //参数也可能是结构体,表,ABAP对象 //一定要分开TRY,因为有些是没有的 if (pMetadata.DataType == RfcDataType.STRUCTURE) { try { //结构体的行项目结构 List <object> elelist = new List <object>(); RfcStructureMetadata strucmeta = pMetadata.ValueMetadataAsStructureMetadata; for (int f = 0; f < strucmeta.FieldCount; f++) { RfcFieldMetadata fieldm = strucmeta[f]; elelist.Add(fieldm); } paralist.Objects.Add(pMetadata.Name, elelist); } catch (Exception) { // throw new SAPException(ee.Message); } } if (pMetadata.DataType == RfcDataType.TABLE) { //表结构的行项目结构 List <object> tbllist = new List <object>(); RfcTableMetadata tablem = pMetadata.ValueMetadataAsTableMetadata; for (int t = 0; t < tablem.LineType.FieldCount; t++) { RfcFieldMetadata fieldm = tablem.LineType[t]; tbllist.Add(fieldm); } paralist.Objects.Add(pMetadata.Name, tbllist); } if (pMetadata.DataType == RfcDataType.CLASS) { //abap object 属性 List <object> attlist = new List <object>(); RfcAbapObjectMetadata abapitem = pMetadata.ValueMetadataAsAbapObjectMetadata; for (int o = 0; o < abapitem.AttributeCount; o++) { RfcAttributeMetadata abapobject = abapitem[o]; attlist.Add(abapobject); } paralist.Objects.Add(pMetadata.Name, attlist); } } return(paralist); } catch (Exception ee) { throw new SAPException(ee.Message); } }