DBReader/Classes/TableReader

/**
*
* DBReader 1.0, Access to SAP tables using SAP's .Net Connector.
*
* Author: hardteck
* Email: hardteck@web.de
*
* Last modified: Fr, 17. Okt 2003, 11:31:36
*/
namespace SAPReader {
using System;
using System.Collections;
using SAP.Connector;

///
/// Reads a table from SAP by using RFC_READ_TABLE call to SAP.
/// See documentation for RFC_READ_TABLE Function Module at
/// your SAP system for further details (Transaction SE37).
/// The result is stored in a
/// column by column.
///
public class TableReader {
///
/// Static field to indicate NoData mode.
///
public static string NO_DATA = "X";
///
/// The proxy to SAP.
///
protected SAPReader.Proxy proxy = null;
///
/// The table to read from.
///
protected string qTable = "";
///
/// A delimiter to separate fieldentries in the returned
/// TAB512Table structure.
///
protected string delim = "";
///
/// Indicates whether table data should be returned from SAP
/// or only table meta information.
///
protected string noData = TableReader.NO_DATA;
///
/// Number of maximum number of rows to return.
/// This is useful, if the table content is huge to limit
/// transfer time. Along with rowSkip, all the table content
/// could be accessed successively.
///
protected int rowCount = 0;
///
/// Number of rows to skip.
///
///
protected int rowSkip = 0;
///
/// The structure to hold the returned table content.
///
protected SAPReader.SAPKernel.TAB512Table data = null;
///
/// The desired fields.
///
protected SAPReader.SAPKernel.RFC_DB_FLDTable fields = null;
///
/// The text of the (SQL-)WHERE clause.
///
protected SAPReader.SAPKernel.RFC_DB_OPTTable options = null;
///
/// The result is stored into a ResultSet column-wise.
///
protected SAPReader.ResultSet results = null;

///
/// The constructor to construct from a ConnectionInfo object.
///
/// A ConnectionInfo
public TableReader(SAPReader.ConnectionInfo conInfo) {
this.proxy = new Proxy(conInfo);
this.init();
}
///
/// The constructor to construct from a Destination object.
///
/// The destination to use.
public TableReader(SAP.Connector.Destination dest) {
this.proxy = new Proxy(dest);
this.init();
}
///
/// The constructor.
///
/// The proxy to use.
public TableReader(SAPReader.Proxy proxy) {
this.proxy = proxy;
this.init();
}
///
/// Initialization.
///
private void init(){
this.data = new SAPKernel.TAB512Table();
this.fields = new SAPKernel.RFC_DB_FLDTable();
this.options = new SAPKernel.RFC_DB_OPTTable();
this.results = new SAPReader.ResultSet();
}
///
/// Return the ResultSet.
///
/// The stored data column-wise.
public SAPReader.ResultSet getResultSet() {
return this.results;
}
///
/// Return or set the Delimiter.
///
public string Delimiter {
get{return this.delim;}
set{this.delim = value;}
}
///
/// Return or set the rowCount.
///
public int RowCount {
set{this.rowCount = value;}
get{return this.rowCount;}
}
///
/// Return or set the rowSkip.
///
public int RowSkip {
set{this.rowSkip = value;}
get{return this.rowSkip;}
}
///
/// Return or set the proxy.
///
public SAPReader.Proxy Proxy {
get{return this.proxy;}
set{this.proxy = value;}
}
///
/// Return or set the NoData indicator.
///
public bool NoData {
set{
if(value == true){
this.noData = TableReader.NO_DATA;
}
else{
this.noData = "";
}
}
get{return this.noData == TableReader.NO_DATA;}
}
///
/// Return or set the QueryTable.
///
public string QueryTable {
get{return this.qTable;}
set{this.qTable = value;}
}
///
/// Return the selected fields.
///
public SAPReader.SAPKernel.RFC_DB_FLDTable Fields {
get{return this.fields;}
}
///
/// Add a specified field.
///
/// Fieldname.
/// The corresponding field structure.
public SAPReader.SAPKernel.RFC_DB_FLD addQueryField(string fieldName){
SAPKernel.RFC_DB_FLD field = new SAPKernel.RFC_DB_FLD();
field.Fieldname = fieldName;
this.fields.Add(field);
return field;
}
///
/// Add a part of the WHERE clause.
///
/// Text to be added.
public void addWhereClause(string clauseText){
SAPKernel.RFC_DB_OPT clause = new SAPKernel.RFC_DB_OPT();
clause.Text = clauseText;
this.options.Add(clause);
}
///
/// Reset the Set.
///
public void reset(){
this.qTable = "";
this.delim = "";
this.noData = "";
this.rowCount = 0;
this.rowSkip = 0;
this.clear();
}
///
/// Clear the set.
///
public void clear(){
this.data.Clear();
this.fields.Clear();
this.options.Clear();
this.results.Clear();
}
///
/// Read from SAP using RFC calls to the SAP side
/// function module RFC_READ_TABLE.
///
/// ABAP Errorcode.
public string read(){
if(this.proxy.connected() == false){
this.proxy.connectSAP();
}

try {
// Force the proxy to make the rfc call synchronously
// See documentation for RFC_READ_TABLE Function Module at
// your SAP system for further details (Transaction SE37).
this.proxy.SAPProxy.Rfc_Read_Table(this.delim, this.noData,
this.qTable, this.rowCount, this.rowSkip,
ref this.data, ref this.fields, ref this.options);

// Check if data found
if( this.NoData == false && this.data.Count == 0 ){
return "No data found.";
}

// NoData set
if(this.NoData == true){
for( int i = 0; i < this.fields.Count; i++){
this.results.addEntry(this.fields[i], "NoData");
}
}
// NoData not set
// Save data to the column-wise ResultSet.
else if(this.data.Count > 0 ){
//Loop over data rows
for( int i = 0; i < this.data.Count; i++){
//Loop over fields
for(int j = 0; j < this.fields.Count; j++){
string val = this.parseTableRow(this.fields[j], this.data[i]);
this.results.addEntry(this.fields[j], val);
}
}
}//else
}
catch (SAP.Connector.RfcSystemException ex) {
System.Text.StringBuilder msg = new System.Text.StringBuilder();
msg.Append("Table: " + this.qTable);
foreach(SAPKernel.RFC_DB_FLD field in this.fields){
msg.Append(" Fields: " + field.Fieldname);
}
foreach(SAPKernel.RFC_DB_OPT opt in this.options){
msg.Append(" Options: " + opt.Text);
}
Console.WriteLine(msg + " "
+ "Error calling SAP RFC " + ex.ToString() + " " + ex.ErrorCode,
"Problem with SAP"
);

return ex.ErrorCode;
}
catch(SAP.Connector.RfcAbapException ex){
return ex.ErrorCode;
}
this.proxy.disConnectSAP();
return null;
}
///
/// Returns the value of the specified field in the current table row.
///
/// The field which value is desired.
/// The current row of data.
/// Returns the value of the field in the specified row.
public string parseTableRow(SAPReader.SAPKernel.RFC_DB_FLD field, SAPReader.SAPKernel.TAB512 dataRow ){
// Length of the field
int len = int.Parse(field.Length);
// Position where the fields value starts in the data row
int offset = int.Parse(field.Offset);

// The data row, containing all the field values concatenated by SAP's rfc_read_table
string row = dataRow.Wa;
string retValue = "";

try{
if(offset < row.Length){
// Read the field's value starting at the position specified by offset.
if(offset + len > row.Length)
// Read until the end of the row string
retValue = dataRow.Wa.Substring(offset);
else
// Read only len characters, otherwise.
retValue = dataRow.Wa.Substring(offset,len);
}
}catch(System.ArgumentOutOfRangeException e){
Console.WriteLine(e.ToString());
Environment.Exit(0);
}
return retValue;
}
}
}

[@more@]TableReader

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/232040/viewspace-969003/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/232040/viewspace-969003/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值