最近我正在开发一个需要从SAP表获取数据到MICROSOFT SQL表的项目,我已经完成了连接部分,并且还将SAP结构值拉入ASP.NET。
我的问题是如何将结构保存到Sql表中?
这是我的代码;
RfcDestination mydestination = RfcDestinationManager.GetDestination("rfcecp");
RfcRepository myrepository = mydestination.Repository;
IRfcFunction EHSIMSFM = myrepository.CreateFunction("ZEHSIMS");
EHSIMSFM.Invoke(mydestination);
IRfcTable positable = EHSIMSFM.GetTable("POSITIONTAB1");
if (positable.RowCount > 0)"THIS IS THE SAP STRUCTURE WITH ROW COUNT '300'."
{
posid.Text = "working";
}
else
{
posid.Text = "notworking";
}
}
catch (Exception ej)
{
posid.Text = ej.Message;
}
答案 0 :(得分:1)
您可以通过NCo 3 SAP Connector从返回的IRfcTable收集所有信息。
以下是从IRfcTable对象遍历每一行(IRfcStructure)的方法..
foreach (IRfcStructure row in rfcTable)
{
for (int element = 0; element < rfcTable.ElementCount; element++)
{
RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
// row.GetString(metadata.Name); // get value from row's field
}
// You have your data from a row here..
}
示例:以下是将IRfcTable转换为DataTable的代码。
public static DataTable CreateDataTable(IRfcTable rfcTable)
{
var dataTable = new DataTable();
for (int element = 0; element < rfcTable.ElementCount; element++)
{
RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
dataTable.Columns.Add(metadata.Name);
}
foreach (IRfcStructure row in rfcTable)
{
DataRow newRow = dataTable.NewRow();
for (int element = 0; element < rfcTable.ElementCount; element++)
{
RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
newRow[metadata.Name] = row.GetString(metadata.Name);
}
dataTable.Rows.Add(newRow);
}
return dataTable;
}
同样,您可以将数据添加到MSSQL DB中。