C#操作SAS

SAS数据分析,C#操作的一些辅助代码(重发)。

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        
/**//// 
        
/// 默认创建本地连接
        
/// 

        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        
/**//// 
        
/// 用户密码
        
/// 

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

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
        
/// 用户名
        
/// 

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

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
        
/// 服务器地址
        
/// 

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

        
private short _serverPort;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
        
/// 连接端口号
        
/// 

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


        
private Protocols _protocals;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
        
/// 连接协议
        
/// 

        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);
        }

    }

}

 

 

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        
/**//// 
        
/// 执行出错事件
        
/// 

        public event SASErrorEventHandler OnError;

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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
        
/// 运行SAS程序
        
/// 

        
/// 

        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        
/**//// 
        
/// 运行SAS文件
        
/// 

        
/// 

        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        
/**//// 
        
/// 查询结果集
        
/// 

        
/// 分配逻辑库引用名
        
/// 
        
/// 

        public System.Data.DataSet GetResults(string libname, string command)
ExpandedSubBlockStart.gifContractedSubBlock.gif        

你可能感兴趣的:([随笔分类]Other)