`

C#操作SAS

    博客分类:
  • .NET
阅读更多

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  SASWorkspaceManager;

namespace  SASShare
{
    
public   class  Connection
    
{
        
///   <summary>
        
///  默认创建本地连接
        
///   </summary>

         public  Connection()
            : 
this ( null 0 "" "" , Protocols.ProtocolBridge)
        
{}

        
public  Connection( string  serverIP,  short  serverPort,  string  userName,  string  userPass, Protocols protocols)
        
{
            
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;
        
///   <summary>
        
///  用户密码
        
///   </summary>

         public   string  UserPassword
        
{
            
get   return  _userPassword; }
            
set   { _userPassword  =  value; }
        }

        
///   <summary>
        
///  用户名
        
///   </summary>

         public   string  UserName
        
{
            
get   return  _userName; }
            
set   { _userName  =  value; }
        }

        
///   <summary>
        
///  服务器地址
        
///   </summary>

         public   string  ServerIP
        
{
            
get   return  _serverIP; }
            
set   { _serverIP  =  value; }
        }

        
private   short  _serverPort;
        
///   <summary>
        
///  连接端口号
        
///   </summary>

         public   short  ServerPort
        
{
            
get   return  _serverPort; }
            
set   { _serverPort  =  value; }
        }


        
private  Protocols _protocals;
        
///   <summary>
        
///  连接协议
        
///   </summary>

         public  Protocols Protocals
        
{
            
get   return  _protocals; }
            
set   { _protocals  =  value; }
        }


        
public  SASProvider CreateSASProvider()
        
{
            
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
{
    
public   class  SASProvider
    
{
        
private  IWorkspace workSpace;
        
public  IWorkspace WorkSpace
        
{
            
get   return  workSpace; }
            
set   { workSpace  =  value; }
        }


        
private  Connection _connection;

        
public  Connection Connection
        
{
            
get   return  _connection; }
        }

        
string  message;

        WorkspaceManager _workspaceManager 
=   new  SASWorkspaceManager.WorkspaceManager();

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

        
///   <summary>
        
///  执行出错事件
        
///   </summary>

         public   event  SASErrorEventHandler OnError;

        
internal  SASProvider(Connection connection)
        
{
            _connection 
=  connection;
        }


        
///   <summary>
        
///  运行SAS程序
        
///   </summary>
        
///   <param name="sasCommand"></param>

         public   void  Submit( string  sasCommand)
        
{
            IServerDef2 _serverDef 
=   null ;
            
if  ( ! string .IsNullOrEmpty(_connection.ServerIP))
            
{
                _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
            
{
                workSpace.LanguageService.Submit(sasCommand);
            }

            
catch  (Exception e)
            
{
                FireEvent(
this , e);
            }

            
finally
            
{
                workSpace.Close();
            }

        }


        
///   <summary>
        
///  运行SAS文件
        
///   </summary>
        
///   <param name="path"></param>

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

        }


        
///   <summary>
        
///  查询结果集
        
///   </summary>
        
///   <param name="libname"> 分配逻辑库引用名 </param>
        
///   <param name="command"></param>
        
///   <returns></returns>

         public  System.Data.DataSet GetResults( string  libname,  string  command)
        
{
            System.Data.DataSet ds 
=   new  System.Data.DataSet();
            OleDbConnection conn 
=  getConnection();
            OleDbCommand cmd 
=   new  OleDbCommand(libname, conn);
            OleDbDataAdapter da 
=   new  OleDbDataAdapter(command, conn);
            
try
            
{
                conn.Open();
                cmd.ExecuteNonQuery();
                da.Fill(ds);
            }

            
catch  (Exception e)
            
{
                FireEvent(
this , e);
            }

            
finally
            
{
                conn.Close();
            }


            
return  ds;
        }


        
///   <summary>
        
///  查询单个结果
        
///   </summary>
        
///   <param name="libname"> 分配逻辑库引用名 </param>
        
///   <returns></returns>

         public   object  GetResult( string  libname,  string  selectCommand)
        
{
            
object  result  =   null ;
            OleDbConnection conn 
=  getConnection();
            OleDbCommand cmd 
=   new  OleDbCommand(libname, conn);
            
try
            
{
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.CommandText 
=  selectCommand;
                result 
=  cmd.ExecuteScalar();
            }

            
catch  (Exception e)
            
{
                FireEvent(
this , e);
            }

            
finally
            
{
                conn.Close();
            }

            
return  result;
        }

        
///   <summary>
        
///  执行增删改操作
        
///   </summary>
        
///   <param name="libname"> 分配逻辑库引用名 </param>
        
///   <param name="command"></param>
        
///   <returns></returns>

         public   int  Execute( string  libname,  string  command)
        
{
            
int  result  =   0 ;
            OleDbConnection conn 
=  getConnection();
            OleDbCommand cmd 
=   new  OleDbCommand(libname, conn);
            
try
            
{
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.CommandText 
=  command;
                result 
=  cmd.ExecuteNonQuery();
            }

            
catch  (Exception e)
            
{
                FireEvent(
this , e);
            }

            
finally
            
{
                conn.Close();
            }

            
return  result;
        }


        
private  OleDbConnection getConnection()
        
{
            
string <span style

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics