zoukankan
html css js c++ java
C#操作SAS
SAS数据分析,C#操作的一些辅助代码(重发)。
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
connString;
if
(
string
.IsNullOrEmpty(Connection.ServerIP))
connString
=
"
Provider=sas.IOMProvider.1; Data Source=_LOCAL_ ;
"
;
else
{
connString
=
string
.Format(
"
Provider=sas.IOMProvider.1;Data Source={0};SAS Port={1};SAS Machine DNS Name={2};SAS Protocol={3};User ID={4};Password={5};
"
, Connection.ServerIP, Connection.ServerPort, Connection.ServerIP, Convert.ToInt32(Connection.Protocals), Connection.UserName, Connection.UserPassword);
}
return
new
OleDbConnection(connString);
}
private
void
FireEvent(
object
sender, Exception e)
{
if
(OnError
!=
null
)
{
OnError(sender,
new
SASErrorEventArgs
{ Exception
=
e }
);
}
}
}
public
class
SASErrorEventArgs
{
public
Exception Exception;
}
}
查看全文
相关阅读:
[atARC088F]Christmas Tree
[atARC109F]1D Kingdom Builder
[luogu4259]寻找车位
[atARC087F]Squirrel Migration
[atARC087E]Prefix-free Game
[atARC110F]Esoswap
[atARC110E]Shorten ABC
[atARC084D]Small Multiple
[atARC083F]Collecting Balls
[hihocoder][Offer收割]编程练习赛49
原文地址:https://www.cnblogs.com/heros/p/1505844.html
最新文章
96. Unique Binary Search Trees
网络爬虫入门——案例三:爬取大众点评的商户信息
网络爬虫入门——案例二:爬取教务系统中的学生成绩
网络爬虫入门——案例一:爬取百度贴吧帖子
James Munkres Topology: Theorem 20.4
James Munkres Topology: Theorem 19.6
A tuple is defined as a function
Constructing continuous functions
James Munkres Topology: Sec 18 Exer 12
Concept of function continuity in topology
热门文章
James Munkres Topology: Theorem 16.3
Barber paradox
Metaphor of topological basis and open set
Consideration about improving mathematics study
[loj2850]无进位加法
[atAGC045A]Xor Battle
[luogu7116]微信步数
[cf710F]String Set Queries
[atAGC046E]Permutation Cover
[atARC089F]ColoringBalls
Copyright © 2011-2022 走看看