1.导入Interop.SAPFunctionsOCX.dll Interop.SAPlogonCtrl.dll Interop.SAPTableFactoryCtrl
2.
com端口,缺点:占用线程,单线程
private void getCurrentStock(object obj)
{
SPItemEventProperties Pro = (SPItemEventProperties)obj;
SPListItem item = Pro.ListItem;
SAPFunctionsOCX.SAPFunctionsClass func = VStoSAP.getSAPFunctionsClass("", "");
if (func != null)
{
string MaterialNo = getSPFieldTextValue(item["Material"]);
string Plant = getSPFieldTextValue(item["Plant"]);
SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("ZRFC_MM_MASTER_MRB");
SAPFunctionsOCX.IParameter IV_MATNR = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("IV_MATNR"); //Material No.
SAPFunctionsOCX.IParameter IV_WERKS = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("IV_WERKS"); //Plant
IV_MATNR.Value = MaterialNo;
IV_WERKS.Value = Plant;
ifunc.Call();
SAPFunctionsOCX.IParameter EV_MAKTX = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("EV_MAKTX");
SAPFunctionsOCX.IParameter EV_PRICE = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("EV_PRICE");
SAPFunctionsOCX.IParameter EV_MEINS = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("EV_MEINS");
SAPFunctionsOCX.IParameter EV_DSNAM = (SAPFunctionsOCX.IParameter)ifunc.get_Imports("EV_DSNAM");
if (EV_MAKTX != null && EV_MAKTX.Value != null)
{
item["Description"] = EV_MAKTX.Value.ToString();
}
if (EV_PRICE != null && EV_PRICE.Value != null)
{
item["Unit Price"] = Convert.ToInt32(EV_PRICE.Value);
}
if (EV_MEINS != null && EV_MEINS.Value != null)
{
item["Unit"] = EV_MEINS.Value.ToString();
}
if (EV_DSNAM != null && EV_DSNAM.Value != null)
{
item["Project"] = EV_DSNAM.Value.ToString();
}
DisableEventFiring();
item.SystemUpdate();
EnableEventFiring();
}
}
3.Added的事件中处理:
#region 方法三:com端口,缺点:占用线程,单线程
SPItemEventProperties Pro = properties;
//Thread mrbThread = new Thread(new ThreadStart(getCurrentStock));
Thread mrbThread = new Thread(getCurrentStock);
mrbThread.SetApartmentState(ApartmentState.STA);
//mrbThread.Start();
mrbThread.Start(properties);
mrbThread.Join();
#endregion
4.VStoSAP类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Net;
using System.Configuration;
namespace EventHandler1888
{
public class VStoSAP
{
private static string getDefaultServerName()
{
string hostName = Dns.GetHostName();
if (hostName.ToUpper() == "正式服务器名字")
{
return "PRD";
}
else
{
return "QAS";
}
}
static string FindConfigStringValue(string strSrc, string strFind)
{
string ss = strSrc.ToUpper();
strFind = strFind.ToUpper();
int nStart = ss.IndexOf(strFind, 0);
if (nStart < 0)
return null;
if (nStart > 0 && ss[nStart - 1] != ';')//关键字之前必须有分号或是开始位置
return null;
nStart = ss.IndexOf("=", nStart);
if (nStart < 0)
return null;
nStart++;
int nEnd = ss.IndexOf(";", nStart);
if (nEnd < 0)
{
return strSrc.Substring(nStart, strSrc.Length - nStart);
}
else
{
return strSrc.Substring(nStart, nEnd - nStart);
}
}
internal static SAPFunctionsOCX.SAPFunctionsClass getSAPFunctionsClass(string Server, string Debug)
{
string connStr = "";
GetClient(ref Server, Debug, ref connStr);
SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass();
login.ApplicationServer = FindConfigStringValue(connStr, "Server");
login.Client = FindConfigStringValue(connStr, "Client");
login.Language = FindConfigStringValue(connStr, "Language");
login.User = FindConfigStringValue(connStr, "User");
login.Password = FindConfigStringValue(connStr, "Psw");
login.SystemNumber = Convert.ToInt16(FindConfigStringValue(connStr, "SysNo"));
try
{
SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection();
//conn.Language = "ZH";
//conn.CodePage = "8400";
if (conn.Logon(0, true))
{
SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();
func.Connection = conn;
return func;
}
else
{
return null;
}
}
catch (Exception)
{
return null;
}
}
private static void GetClient(ref string Server, string Debug, ref string connStr)
{
if (string.IsNullOrEmpty(Server))
{
Server = getDefaultServerName();
}
if (Server.ToUpper() == "PRD")
{
if (Debug == "1")
{
connStr = ConfigurationManager.AppSettings["SAPPRDDebug"];
}
else
{
connStr = ConfigurationManager.AppSettings["SAPPRD"];
}
}
if (Server.ToUpper() == "QAS")
{
if (Debug == "1")
{
connStr = ConfigurationManager.AppSettings["SAPQASDebug"];
}
else
{
connStr = ConfigurationManager.AppSettings["SAPQAS"];
}
}
}
/// <summary>
/// 格式化SAPTable为DT
/// </summary>
/// <param name="SAPTable">SAP Table</param>
/// <returns></returns>
internal static DataTable convetTable(SAPTableFactoryCtrl.Table SAPTable)
{
try
{
int count = SAPTable.ColumnCount;
DataTable dt = new DataTable();
dt.Columns.Add("Index", typeof(int));
for (int i = 1; i <= count; i++)
{
string columnName = SAPTable.get_ColumnName(i);
dt.Columns.Add(columnName, typeof(string));
}
int countRows = SAPTable.RowCount;
for (int i = 1; i <= countRows; i++)
{
DataRow dr = dt.NewRow();
for (int j = 1; j < dt.Columns.Count; j++)
{
dr[j] = SAPTable.get_Cell(i, SAPTable.get_ColumnName(i)).ToString();
}
dt.Rows.Add(dr);
}
return dt;
}
catch (Exception)
{
return null;
}
}
}
}
|
请发表评论