C#操作SAS

SAS數據分析,C#操作的一些輔助代碼(重發)。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SASWorkspaceManager;

namespace SASShare
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class Connection
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 默認創建本地連接
        
/// </summary>

        public Connection()
            : 
this(null0"""", Protocols.ProtocolBridge)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{}

        
public Connection(string serverIP, short serverPort, string userName, string userPass, Protocols protocols)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this._serverIP = serverIP;
            
this._serverPort = serverPort;
            
this._userName = userName;
            
this._userPassword = userPass;
            
this._protocals = protocols;
        }


        
private string _serverIP = string.Empty, _userName = string.Empty, _userPassword = string.Empty;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 用戶密碼
        
/// </summary>

        public string UserPassword
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _userPassword; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _userPassword = value; }
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 用戶名
        
/// </summary>

        public string UserName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _userName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _userName = value; }
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 服務器地址
        
/// </summary>

        public string ServerIP
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _serverIP; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _serverIP = value; }
        }

        
private short _serverPort;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 連接端口號
        
/// </summary>

        public short ServerPort
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _serverPort; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _serverPort = value; }
        }


        
private Protocols _protocals;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 連接協議
        
/// </summary>

        public Protocols Protocals
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _protocals; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _protocals = value; }
        }


        
public SASProvider CreateSASProvider()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return new SASProvider(this);
        }

    }

}

 

 

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SASWorkspaceManager;
using SAS;
using System.IO;
using System.Data.OleDb;

namespace SASShare
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class SASProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private IWorkspace workSpace;
        
public IWorkspace WorkSpace
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return workSpace; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { workSpace = value; }
        }


        
private Connection _connection;

        
public Connection Connection
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _connection; }
        }

        
string message;

        WorkspaceManager _workspaceManager 
= new SASWorkspaceManager.WorkspaceManager();

        
public delegate void SASErrorEventHandler(object sender, SASErrorEventArgs args);

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 執行出錯事件
        
/// </summary>

        public event SASErrorEventHandler OnError;

        
internal SASProvider(Connection connection)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            _connection 
= connection;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 運行SAS程序
        
/// </summary>
        
/// <param name="sasCommand"></param>

        public void Submit(string sasCommand)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            IServerDef2 _serverDef 
= null;
            
if (!string.IsNullOrEmpty(_connection.ServerIP))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                _serverDef 
= new SASWorkspaceManager.ServerDefClass();
                _serverDef.Port 
= Connection.ServerPort;
                _serverDef.Protocol 
= Connection.Protocals;
                _serverDef.MachineDNSName 
= Connection.ServerIP;
            }

            workSpace 
= _workspaceManager.Workspaces.CreateWorkspaceByServer("_LOCAL_", SASWorkspaceManager.Visibility.VisibilityProcess, _serverDef, _connection.UserName, _connection.UserPassword, out message);
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                workSpace.LanguageService.Submit(sasCommand);
            }

            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                FireEvent(
this, e);
            }

            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                workSpace.Close();
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 運行SAS文件
        
/// </summary>
        
/// <param name="path"></param>

        public void RunSasFile(string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (File.Exists(path))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
string command = File.ReadAllText(path);
                Submit(command);
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 查詢結果集
        
/// </summary>
        
/// <param name="libname">分配邏輯庫引用名</param>
        
/// <param name="command"></param>
        
/// <returns></returns>

        public System.Data.DataSet GetResults(string libname, string command)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章