我调用RFC函数的代码如下
private void TestTable_Click(object sender, EventArgs e)
{
SAPLogonCtrl.SAPLogonControlClass connctl = new SAPLogonCtrl.SAPLogonControlClass();
//下面的参数值根据实际情况赋值
connctl.Client = "300";
connctl.Language = "ZH";
connctl.ApplicationServer = "10.1.1.30";//Application server IP
connctl.SystemNumber = 00;
connctl.User = "mes";
connctl.Password = "intmes";
SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)connctl.NewConnection();
//登陆
if (conn.Logon(null, true))
MessageBox.Show("Successful");
SAPFunctionsOCX.SAPFunctionsClass functions = new SAPFunctionsOCX.SAPFunctionsClass();
functions.Connection = conn;
//这里就可以传入Function Name
SAPFunctionsOCX.Function fucntion = (SAPFunctionsOCX.Function)functions.Add("RFC_READ_TABLE");
//这里是传入值参数
SAPFunctionsOCX.Parameter parameter1 = (SAPFunctionsOCX.Parameter)fucntion.get_Exports("QUERY_TABLE");
parameter1.Value = "ZMESBG";
SAPFunctionsOCX.Parameter parameter2 = (SAPFunctionsOCX.Parameter)fucntion.get_Exports("DELIMITER");
parameter2.Value = ";";
//这里是传入表的参数,这里有两个表OPTIONS和FIELDS
SAPTableFactoryCtrl.Tables Tables1 = (SAPTableFactoryCtrl.Tables)fucntion.Tables;
SAPTableFactoryCtrl.Table table0 = (SAPTableFactoryCtrl.Table)Tables1.get_Item("OPTIONS");
table0.AppendGridData(1, 1, 1, "LGORT = 'Z000'");
SAPTableFactoryCtrl.Table table1 = (SAPTableFactoryCtrl.Table)Tables1.get_Item("FIELDS");
table1.AppendGridData(1, 1, 1, "ZMESBGID");
table1.AppendGridData(2, 1, 1, "WERKS");
table1.AppendGridData(3, 1, 1, "BUDAT");
table1.AppendGridData(4, 1, 1, "CONF_CNT");
table1.AppendGridData(5, 1, 1, "ERZET");
////这行报错,错误提示 Bad index
table1.AppendGridData(6, 1, 1, "ERDAT");
table1.AppendGridData(7, 1, 1, "SERNR");
table1.AppendGridData(8, 1, 1, "LGORT");
//调用函数,并读取数据
if (fucntion.Call())
{
Tables1 = (SAPTableFactoryCtrl.Tables)fucntion.Tables;
SAPTableFactoryCtrl.Table talbe2 = (SAPTableFactoryCtrl.Table)Tables1.get_Item("DATA");
MessageBox.Show(talbe2.get_Cell(1, 1).ToString());
}
//退出登陆
conn.Logoff();
}
////这行报错,错误提示 Bad index
table1.AppendGridData(6, 1, 1, "ERDAT");
table1.AppendGridData(7, 1, 1, "SERNR");
table1.AppendGridData(8, 1, 1, "LGORT");
如果把那3行注释,程序是能正常执行的,但是实际情况是需要更多的行数的
谁能给些指导意见?