public void test()
{
SAPLogonControlClass connctl = new SAPLogonControlClass();
//下面的参数值根据实际情况赋值
connctl.Client = "集团号";
connctl.Language = "ZH";
connctl.ApplicationServer = "服务器IP";
connctl.SystemNumber = 00;
connctl.User = "SAP登录名";
connctl.Password = "SAP密码";
Connection conn = (Connection)connctl.NewConnection();
//登陆
if (conn.Logon(null, true))
{
SAPFunctionsClass functions = new SAPFunctionsClass();
functions.Connection = conn;
//这里就传入函数名称RFC_READ_TABLE
Function fucntion = (Function)functions.Add("RFC_READ_TABLE");
//这里是传入值参数 QUERY_TABLE 就是要查询的表名 DELIMITER就是字段间的间隔符号,因为函数的返回参数不是表格形式
Parameter parameter1 = (Parameter)fucntion.get_Exports("QUERY_TABLE");
parameter1.Value = "BSEG";
Parameter parameter2 = (Parameter)fucntion.get_Exports("DELIMITER");
parameter2.Value = "@";
//这里是传入表的参数,这里有两个表OPTIONS和FIELDS;OPTION表是存放查询条件的,为空的话是不限制条件,FIELDS是存放你想要查询的字段,按理说可以为空,但是试过不行,只能设置要取的字段
Tables Tables1 = (Tables)fucntion.Tables;
Table table0 = (Table)Tables1.get_Item("OPTIONS");
table0.AppendGridData(1, 1, 1, "");//设置条件,例如(MANDT=‘100’)
Table talble1 = (Table)Tables1.get_Item("FIELDS");
talble1.AppendGridData(1, 1, 1, "MANDT");//
talble1.AppendGridData(2, 1, 1, "BUKRS");//
talble1.AppendGridData(3, 1, 1, "BELNR");//
talble1.AppendGridData(4, 1, 1, "GJAHR");//
talble1.AppendGridData(5, 1, 1, "BUZEI");//
talble1.AppendGridData(1, 2, 1, "SHKZG");//
talble1.AppendGridData(2, 2, 1, "MENGE");//
talble1.AppendGridData(3, 2, 1, "MEINS");//
talble1.AppendGridData(4, 2, 1, "EBELN");//
talble1.AppendGridData(5, 2, 1, "EBELP");//
//调用函数,并读取数据
if (fucntion.Call())
{
Tables1 = (Tables)fucntion.Tables;
//获取函数返回内容
Table talbe2 = (Table)Tables1.get_Item("DATA");
MessageBox.Show(talbe2.get_cell(1,1).ToString());
或者
MessageBox.Show(talbe2.get_cell(1,"MANDT").ToString());
}
//退出登陆
conn.Logoff();
}
C# 调用 SAP RFC 方法 收藏
添加SAP安装程序的四个dll文件引用:
Interop.SAPBAPIControlLib.dll
Interop.SAPFunctionsOCX.dll
Interop.SAPLogonCtrl.dll
Interop.SAPTableFactoryCtrl.dll
调用方法体:
private void GetMateriel()
{
string number = this.txtNumber.Text.Trim();
string desc = this.txtDesc.Text.Trim();
Config config = new Config();
SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass();
login.ApplicationServer = config.Server;
login.Client = config.Client;
login.Language = config.Language;
login.User = config.User;
login.Password = config.Password;
login.SystemNumber = config.Number;
SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection();
DataSet ds = new DataSet();
DataTable table = new DataTable();
table.Columns.Add("Number", typeof(string));
table.Columns.Add("Desc1", typeof(string));
table.Columns.Add("Desc2", typeof(string));
table.Columns.Add("Desc3", typeof(string));
table.Columns.Add("Uint", typeof(string));
if (conn.Logon(0, true))
{
SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();
func.Connection = conn;
SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("Z_MATERIAL_APPLICATION"); //(Z_MATERIAL_APPLICATION) SAP RFC 名称
SAPFunctionsOCX.IParameter gclient = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("I_WERKS"); //(I_WERKS)输入参数
gclient.Value = Factory; //(Factory)对参数赋值
SAPFunctionsOCX.IParameter matnr = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("I_MATNR");
matnr.Value = number;
SAPFunctionsOCX.IParameter maktx = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("I_MAKTX");
maktx.Value = desc;
ifunc.Call();
SAPTableFactoryCtrl.Tables tables = (SAPTableFactoryCtrl.Tables)ifunc.Tables;
SAPTableFactoryCtrl.Table ENQ = (SAPTableFactoryCtrl.Table)tables.get_Item("PO_TAB"); (PO_TAB)输出表名
for (int i = 1; i <= ENQ.RowCount; i++)
{
DataRow dr = table.NewRow();
dr[0] = ENQ.get_Cell(i, "MATNR");
dr[1] = ENQ.get_Cell(i, "MAKTX");
dr[2] = ENQ.get_Cell(i, "MAKTX2");
dr[3] = ENQ.get_Cell(i, "MAKTX3");
dr[4] = ENQ.get_Cell(i, "MEINS");
table.Rows.Add(dr);
}
ds.Tables.Add(table);
}
conn.Logoff();
this.Repeater1.DataSource = ds.Tables[0];
this.DataBind();
this.lblCount.Text = "共找到" + ds.Tables[0].Rows.Count.ToString() + "條記錄";
ScriptManager.RegisterStartupScript(btnSAP, this.GetType(), "", "$(document).ready( function (){ jQuery.page('page',10);} )", true);
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/robaot/archive/2009/10/20/4704127.aspx
另一篇:C# 连接SAP,并调用RFC函数 (基于Com组件) 收藏
1) 新建工程后,首先添加3个Com组建的引用,分别为 SAPFunctionsOCX,SAPLogonCtrl,SAPTableFactoryCtrl;
2) 项目添加引用
using SAPFunctionsOCX;
using SAPLogonCtrl;
using SAPTableFactoryCtrl;
3) 连接并调用RFC
SAPLogonControlClass connctl = new SAPLogonControlClass();
//下面的参数值根据实际情况赋值
connctl.Client = "200";
connctl.Language = "EN";
connctl.ApplicationServer = "xxx.xxx.xxx.xxx";//Application server IP
connctl.SystemNumber = 00;
connctl.User = "XXXX";
connctl.Password = "XXXXXXX";
Connection conn = (Connection)connctl.NewConnection();
//登陆
if (conn.Logon(null,true))
{
MessageBox.Show("Successful");
}
SAPFunctionsClass functions = new SAPFunctionsClass();
functions.Connection = conn;
//这里就可以传入Function Name
Function fucntion = (Function)functions.Add("RFC_READ_TABLE");。
//这里是传入值参数
Parameter parameter1 = (Parameter)fucntion.get_Exports("QUERY_TABLE");
parameter1.Value = "VEKP";
Parameter parameter2 = (Parameter)fucntion.get_Exports("DELIMITER");
parameter2.Value = ";";
//这里是传入表的参数,这里有两个表OPTIONS和FIELDS
Tables Tables1 = (Tables)fucntion.Tables;
Table table0 = (Table)Tables1.get_Item("OPTIONS");
table0.AppendGridData(1, 1, 1, "EXIDV = '00000000001000000022'");
Table talble1 = (Table)Tables1.get_Item("FIELDS");
talble1.AppendGridData(1, 1, 1, "VENUM");
talble1.AppendGridData(2, 1, 1, "VPOBJKEY");
talble1.AppendGridData(3, 1, 1, "VSTEL");
//调用函数,并读取数据
if (fucntion.Call())
{
Tables1 = (Tables)fucntion.Tables;
Table talbe2 = (Table)Tables1.get_Item("DATA");
MessageBox.Show(talbe2.get_Cell(1, 1).ToString());
}
//退出登陆
conn.Logoff();
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/superying/archive/2009/12/10/4977062.aspx
c# 調用sap rfc - [abap开发]
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://qiqeloveyou.blogbus.com/logs/75375580.html
1.C#与数据库访问技术:http://wenku.baidu.com/view/e590c268a98271fe910ef99e.html
2.实现用C#向SAP内表和结构写入数据
appenddatagrid可以替代set_Cell
SAPLogonCtrl.SAPLogonControlClass SapLogon = new SAPLogonCtrl.SAPLogonControlClass();
SapLogon.Client = "800";
SapLogon.Language = "ZH";
SapLogon.User = "";//R3登陆用户名
SapLogon.Password = "";//登陆密码
SapLogon.ApplicationServer = "";//r3服务器
SapLogon.SystemNumber = 0;
//以下建立与R3的通信机制
SAPLogonCtrl.Connection EnterSap =
(SAPLogonCtrl.Connection)SapLogon.NewConnection();//建立连接
if (EnterSap.Logon(0, true) == false)
{
MessageBox.Show("no");
//this.Label1.Text = "aa";
}
else
{
MessageBox.Show("yes");
//this.Label1.Text = "bb";
}
SAPFunctionsOCX.SAPFunctionsClass sapfc = new
SAPFunctionsOCX.SAPFunctionsClass();
SAPFunctionsOCX.IFunction sapfu =
(SAPFunctionsOCX.IFunction)sapfc.Add("ZOA_UPDATE_PARKED_DOC_ZS");
sapfc.Connection = EnterSap;
SAPFunctionsOCX.IStructure stru =
(SAPFunctionsOCX.IStructure)sapfu.get_Exports("zyztt");
stru.set_Value("bldat", "20070205");
stru.set_Value("BUDAT", "20070205");
stru.set_Value("BLART","SA");
stru.set_Value("BUKRS","4100");
stru.set_Value("WAERS","RMB");
SAPTableFactoryCtrl._CSAPTaFacTables impor =
(SAPTableFactoryCtrl._CSAPTaFacTables)sapfu.Tables;
SAPTableFactoryCtrl._CSAPTaFacTable partable =
(SAPTableFactoryCtrl._CSAPTaFacTable)impor.get_Item("ZYZHXM");
partable.AppendRow();
int i=1;
partable.set_Cell(i,"NEWKO",55032500);
partable.set_Cell(i,"NEWBS","40");//执行到这里出现错
误:未处理的“System.Runtime.InteropServices.COMException”类型的异常出现在
mscorlib.dll 中。其他信息: 非选择性的参数。
MessageBox.Show(partable.get_Cell(i,1).ToString());
使用set_Cell地確會出現異常 但是用appenddatagrid可以替代上面用法,本人調用成工
partable.set_Value(i,"KOSTL","J0");
partable.set_Value(i,"WRBTR","40");
partable.set_Value(i,"SGTXT","11月费用");
partable.AppendRow();
i++;
partable.set_Value(i,"NEWBS","50");
partable.set_Value(i,"NEWKO","10010100");
partable.set_Value(i,"ZZCASH","A92");
partable.set_Value(i,"WRBTR","40");
partable.set_Value(i,"SGTXT","11月费用");
sapfu.Call();
EnterSap.Logoff();