C# 通過SAP提供COM組件進行SAP數據的訪問
步驟:
1. 新建C#工程
2. 在菜單中點擊“Project”--〉“Add Reference” ,在彈出窗口的COM列表中選擇SAP Component 組件
3.用SAPLogonCtrl 組件進行SAP的連接
4.連接后以SAPFunctionsOCX 進行SAP的 RFC Function的調用,傳入Function的Import 、Export Parameters,及Table Parameters
執行后以SAPTableFactoryCtrl 接收返回Table數據(注意此表數據無法直接賦予C#之DataTable)需自定義轉換函數
對於SAP之COM尚未深入了解,目前只能進行簡單數據的讀取,對於未能通過SAP以定義Function取得之數據,可在SAP中自定義Function來進行,注意自定義之Function 之 “Processing Type” 需選擇 “Remote Function Call Supported“ ,否則無法進行遠程使用。
以下為CODE:
public void LoginSAP()
{
SAPLogonCtrl.SAPLogonControlClass logon = new SAPLogonCtrl.SAPLogonControlClass();
logon.ApplicationServer = "IPadress"; //SAP系统IP
logon.Client = "**"; //SAP客户端号
logon.Language = "ZF"; //SAP登陆语言
logon.User = "**"; //用户帐号
logon.Password = "**"; //用户密码
logon.SystemNumber = *; //SAP系统编号
SAPLogonCtrl.Connection Conn = (SAPLogonCtrl.Connection)logon.NewConnection();
{
SAPLogonCtrl.SAPLogonControlClass logon = new SAPLogonCtrl.SAPLogonControlClass();
logon.ApplicationServer = "IPadress"; //SAP系统IP
logon.Client = "**"; //SAP客户端号
logon.Language = "ZF"; //SAP登陆语言
logon.User = "**"; //用户帐号
logon.Password = "**"; //用户密码
logon.SystemNumber = *; //SAP系统编号
SAPLogonCtrl.Connection Conn = (SAPLogonCtrl.Connection)logon.NewConnection();
if (Conn.Logon(0, true))
{
//登陆成功
}
else
{
; //登陆失败
}
/// 调用SAP系统函数模块
/// </summary>
/// <param name="strFunName">函数名称</param>
/// <param name="strArgs">输入参数字典</param>
/// <param name="strRetTabs">返回表结果字典</param>
/// <param name="strResult">返回程序运行结果</param>
/// <returns>返回表结果集</returns>
{
//登陆成功
}
else
{
; //登陆失败
}
/// 调用SAP系统函数模块
/// </summary>
/// <param name="strFunName">函数名称</param>
/// <param name="strArgs">输入参数字典</param>
/// <param name="strRetTabs">返回表结果字典</param>
/// <param name="strResult">返回程序运行结果</param>
/// <returns>返回表结果集</returns>
ListDictionary strArgs = new ListDictionary() ;
ListDictionary strRetTabs = new ListDictionary();
ListDictionary strResult = new ListDictionary();
string strFunName="RFC_CUSTOMER_GET";
object customs= new object();
strArgs.Add("KUNNR","*");
strArgs.Add("NAME1","*");
strRetTabs.Add("CUSTOMER_T",customs);
ListDictionary strRetTabs = new ListDictionary();
ListDictionary strResult = new ListDictionary();
string strFunName="RFC_CUSTOMER_GET";
object customs= new object();
strArgs.Add("KUNNR","*");
strArgs.Add("NAME1","*");
strRetTabs.Add("CUSTOMER_T",customs);
try
{
DataSet retDST = new DataSet();
string[] array = new string[strResult.Count];
strResult.Keys.CopyTo(array, 0);
SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();
{
DataSet retDST = new DataSet();
string[] array = new string[strResult.Count];
strResult.Keys.CopyTo(array, 0);
SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();
func.Connection = Conn;
//(1)
SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add(strFunName); //调用函数模块
foreach (string arg in strArgs.Keys)
{
SAPFunctionsOCX.IParameter gclient = (SAPFunctionsOCX.IParameter)ifunc.get_Exports(arg); //取得输入参数
string sssdfd=strArgs[arg].ToString();
gclient.Value = strArgs[arg]; //设置参数值
}
ifunc.Call(); //调用函数模块
//(2)
foreach (string ret in array)
{
SAPFunctionsOCX.IParameter NUMBER = (SAPFunctionsOCX.IParameter)ifunc.get_Imports(ret); //返回程序运行结果
strResult[ret] = NUMBER.Value;
}
//(3)
SAPTableFactoryCtrl.Tables ENQs = (SAPTableFactoryCtrl.Tables)ifunc.Tables; //获取所有Tables
// MessageBox.Show(ifunc.);
foreach (string tab in strRetTabs.Keys)
{
SAPTableFactoryCtrl.Table ENQ = (SAPTableFactoryCtrl.Table)ENQs.get_Item(tab); //返回指定Tables
// MessageBox.Show(ENQ.RowCount.ToString());
//MessageBox.Show(ENQ.ColumnCount.ToString());
//MessageBox.Show(ENQ.get_ColumnName(5));
//dataGrid1.DataSource=dat.Tables[0].DefaultView;
//(1)
SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add(strFunName); //调用函数模块
foreach (string arg in strArgs.Keys)
{
SAPFunctionsOCX.IParameter gclient = (SAPFunctionsOCX.IParameter)ifunc.get_Exports(arg); //取得输入参数
string sssdfd=strArgs[arg].ToString();
gclient.Value = strArgs[arg]; //设置参数值
}
ifunc.Call(); //调用函数模块
//(2)
foreach (string ret in array)
{
SAPFunctionsOCX.IParameter NUMBER = (SAPFunctionsOCX.IParameter)ifunc.get_Imports(ret); //返回程序运行结果
strResult[ret] = NUMBER.Value;
}
//(3)
SAPTableFactoryCtrl.Tables ENQs = (SAPTableFactoryCtrl.Tables)ifunc.Tables; //获取所有Tables
// MessageBox.Show(ifunc.);
foreach (string tab in strRetTabs.Keys)
{
SAPTableFactoryCtrl.Table ENQ = (SAPTableFactoryCtrl.Table)ENQs.get_Item(tab); //返回指定Tables
// MessageBox.Show(ENQ.RowCount.ToString());
//MessageBox.Show(ENQ.ColumnCount.ToString());
//MessageBox.Show(ENQ.get_ColumnName(5));
//dataGrid1.DataSource=dat.Tables[0].DefaultView;
DataSet dat = CoverTable(ENQ) ;
dataGrid1.SetDataBinding(dat,"TSIS_route");
dataGrid1.SetDataBinding(dat,"TSIS_route");
func.RemoveAll();
Conn.Logoff();
}
}
catch (Exception exc)
{
throw (new Exception(exc.Message));
}
catch (Exception exc)
{
throw (new Exception(exc.Message));
}
}