大家好,我有一个要求要满足,我创建了一个 ASP.NET 页面,该页面在页面加载时从 sap 中获取数据。我已经编写了用于在 PAGE LOAD 中获取 SAP 表的代码。
我需要每天自动在后台运行一次页面(无需用户干预),因此它可以自动同步 ASP.NET 和 SAP 之间的信息。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Security;
using System.Security.Authentication;
using First.EHSIMS;
using First.EHSIMS.Dbml;
using First.EHSIMS.Breadcrumb_New;
using SAP.Middleware.Connector;
public class SAPSystemConnect : IDestinationConfiguration
{
public RfcConfigParameters GetParameters(string destinationName)
{
RfcConfigParameters parms = new RfcConfigParameters();
if ("DEV".Equals(destinationName))
{
parms.Add(RfcConfigParameters.AppServerHost, "ECC6");
parms.Add(RfcConfigParameters.SystemNumber, "04");
parms.Add(RfcConfigParameters.User, "sapuser");
parms.Add(RfcConfigParameters.Password, "newmaars1");
parms.Add(RfcConfigParameters.Client, "800");
parms.Add(RfcConfigParameters.Language, "EN");
parms.Add(RfcConfigParameters.PoolSize, "5");
parms.Add(RfcConfigParameters.MaxPoolSize, "10");
parms.Add(RfcConfigParameters.IdleTimeout, "000");
}
return parms;
}
public bool ChangeEventsSupported()
{
//throw new NotImplementedException();
return false;
}
public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
}
public partial class Sap_Connection_sapcon : System.Web.UI.Page
{
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;
}
protected void Page_Load(object sender, EventArgs e)
{
SAPSystemConnect c = new SAPSystemConnect();
RfcDestinationManager.RegisterDestinationConfiguration(c);
RfcDestination rfcDest = null;
rfcDest = RfcDestinationManager.GetDestination("DEV");
try
{
RfcDestination mydestination = RfcDestinationManager.GetDestination("DEV");
RfcRepository myrepository = mydestination.Repository;
IRfcFunction position = myrepository.CreateFunction("ZPOSI");
IRfcTable positable = position.GetTable("POSITIONTAB1");
IRfcTable jobtable = position.GetTable("JOBTAB");
position.Invoke(mydestination);
RfcDestinationManager.UnregisterDestinationConfiguration(c);
if (jobtable.RowCount > 0)
{
DataTable JOBTABLE = CreateDataTable(jobtable);
using (EHSIMSDataContext db = new EHSIMSDataContext())
foreach (DataRow newRow1 in JOBTABLE.Rows)
{
string JOB_ID = newRow1["JOB_ID"].ToString();
JOB34 _job1 = (from job1 in db.JOB34s
where job1.JOB_NO.Equals(JOB_ID)
select job1).SingleOrDefault<JOB34>();
if(_job1 == null)
{
JOB34 _JOB;
_JOB = new JOB34();
Guid g = Guid.NewGuid();
Guid g1 = Guid.NewGuid();
_JOB.JOB_ID = g1;
_JOB.JOB_NO = newRow1["JOB_ID"].ToString();
_JOB.NAME = newRow1["JOB_TXT"].ToString();
db.JOB34s.InsertOnSubmit(_JOB);
db.SubmitChanges();
}
else
{
}
}
}
else
{
posid.Text = "notworking";
}
if (positable.RowCount > 0)
{
DataTable dataTable = CreateDataTable(positable);
using (EHSIMSDataContext db1 = new EHSIMSDataContext())
foreach (DataRow newRow in dataTable.Rows)
{
string POS_ID = newRow["POSITION_ID"].ToString();
POSITION34 _pos1 = (from job in db1.POSITION34s
where job.POSITION_NO.Equals(POS_ID)
select job).SingleOrDefault<POSITION34>();
if (_pos1 == null)
{
string JOB_ID = newRow["JOB_ID"].ToString();
JOB34 _job = (from job in db1.JOB34s
where job.JOB_NO.Equals(JOB_ID)
select job).SingleOrDefault<JOB34>();
POSITION34 _pos;
_pos = new POSITION34();
Guid g = Guid.NewGuid();
Guid g1 = Guid.NewGuid();
_pos.POSITION_ID = g;
_pos.POSITION_NO = newRow["POSITION_ID"].ToString();
_pos.NAME = newRow["POSITION_TXT"].ToString();
_pos.REPORTED_TO = newRow["REPORTED_TO"].ToString();
_pos.JOB_ID = _job.JOB_ID;
db1.POSITION34s.InsertOnSubmit(_pos);
db1.SubmitChanges();
}
else
{
}
}
}
else
{
posid.Text = "notworking";
}
}
catch (Exception ej)
{
posid.Text = ej.Message;
}
}
}