本文就說明在C#中如何編寫代碼來調用SAP中的RFC函數獲取數據。
首先需要引用兩個NCO3.0的DLL
DLL下載地址:http://files.cnblogs.com/mengxin523/SAP_DotNetConnector3.zip
1 //程序代碼頁面引用: 2 using SAP.Middleware.Connector; 3 4 //代碼如下: 5 namespace SAP_RFC 6 7 { 8 9 public partial class Form1 : Form 10 11 { 12 13 string MATNR = string.Empty; 14 15 public Form1() 16 17 { 18 19 InitializeComponent(); 20 21 } 22 23 public void nco() 24 25 { 26 27 IDestinationConfiguration ID = new MyBackendConfig(); 28 29 RfcDestinationManager.RegisterDestinationConfiguration(ID); 30 31 RfcDestination prd = RfcDestinationManager.GetDestination("PRD_000"); 32 33 RfcDestinationManager.UnregisterDestinationConfiguration(ID); 34 35 nco(prd); 36 37 } 38 39 public void nco(RfcDestination prd) 40 41 { 42 43 RfcRepository repo = prd.Repository; 44 45 IRfcFunction companyBapi = repo.CreateFunction("ZRFC_MARA_INFO"); //調用函數名 46 47 companyBapi.SetValue("MATNR", MATNR); //設置Import的參數 48 49 companyBapi.Invoke(prd); //執行函數 50 51 IRfcTable table = companyBapi.GetTable("IT_MARA"); //獲取相應的品號內表 52 53 string MAKTX = companyBapi.GetValue("MAKTX").ToString(); //獲取品名 54 55 DataTable dt = new DataTable(); //新建表格 56 57 dt.Columns.Add("品號"); //表格添加一列 58 59 for (int i = 0; i < table.RowCount; i++) 60 61 { 62 63 table.CurrentIndex = i; //當前內表的索引行 64 65 DataRow dr = dt.NewRow(); 66 67 dr[0] = table.GetString("MATNR"); //獲取表格的某行某列的值 68 69 dt.Rows.Add(dr); //填充該表格的值 70 71 } 72 73 if (MATNR == "") 74 75 { 76 77 for (int i = 0; i < dt.Rows.Count; i++) 78 79 { 80 81 this.comboBox1.Items.Add(dt.Rows[i][0].ToString()); //填充下拉框 82 83 } 84 85 } 86 87 this.label1.Text = MAKTX; //顯示品名 88 89 prd = null; 90 91 repo = null; 92 93 } 94 95 96 97 //登陸SAP前的准備工作 98 99 public class MyBackendConfig : IDestinationConfiguration 100 101 { 102 103 public RfcConfigParameters GetParameters(String destinationName) 104 105 { 106 107 if ("PRD_000".Equals(destinationName)) 108 109 { 110 111 RfcConfigParameters parms = new RfcConfigParameters(); 112 113 parms.Add(RfcConfigParameters.AppServerHost, "192.168.1.3"); //SAP主機IP 114 115 parms.Add(RfcConfigParameters.SystemNumber, "00"); //SAP實例 116 117 parms.Add(RfcConfigParameters.User, "MENGXIN"); //用戶名 118 119 parms.Add(RfcConfigParameters.Password, "5239898"); //密碼 120 121 parms.Add(RfcConfigParameters.Client, "888"); // Client 122 123 parms.Add(RfcConfigParameters.Language, "ZH"); //登陸語言 124 125 parms.Add(RfcConfigParameters.PoolSize, "5"); 126 127 parms.Add(RfcConfigParameters.MaxPoolSize, "10"); 128 129 parms.Add(RfcConfigParameters.IdleTimeout, "60"); 130 131 return parms; 132 133 } 134 135 else return null; 136 137 } 138 139 public bool ChangeEventsSupported() 140 141 { 142 143 return false; 144 145 } 146 147 public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged; 148 149 } 150 151 152 153 private void Form1_Load(object sender, EventArgs e) 154 155 { 156 157 comboBox1.Items.Clear(); 158 159 nco(); 160 161 comboBox1.SelectedIndex = 1; 162 163 } 164 165 //當下拉框索引變化的時候傳遞品號進去查詢出品名出來 166 167 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 168 169 { 170 171 MATNR = comboBox1.Text.ToString(); 172 173 nco(); 174 175 } 176 177 } 178 179 }
我想這個C#代碼很簡單,我就不多做詳細說明了。結果如下:
SAP中品號信息如下:
由此可見數據完全OK,調用成功。
程序在第一次載入的時候有點慢,在鏈接SAP和登陸。后續在下拉框變化的時候就立馬顯示出品名出來了,絲毫沒有任何停頓。第二次鏈接SAP的時候大概是不必在登陸了,SAP系統中已有登陸信息,運行T-CODE:SM04
紅色框中這兩個即是我們的RFC調用所留下的登錄會話。一旦我們的C#程序退出之后,這兩個RFC也就退出了。
如果我們的C#程序是ASP.NET的話,頁面關閉之后這個RFC登錄信息都還在的。除非IIS關閉,否則只有等到SAP系統超時退出這兩個登陸會話了。