您现在的位置是:网站首页> 数据库
SQL Server调用的VC动态库
- 数据库
- 2023-07-21
- 515人已阅读
DLL源码:
#include <stdafx.h>
#include "Winsock2.h"
#pragma comment (lib,"ws2_32.lib")
#define XP_NOERROR 0
#define XP_ERROR 1
#define MAXCOLNAME 25
#define MAXNAME 25
#define MAXTEXT 255
#ifdef __cplusplus
extern "C" {
#endif
RETCODE __declspec(dllexport) xp_Hello(SRV_PROC *srvproc);
#ifdef __cplusplus
}
#endif
void SendMsg(BYTE *pData,int L)
{
//初始化SOCKET
WSADATA wsaData;
int iRet=WSAStartup(MAKEWORD(2,1),&wsaData);
SOCKET UDPSocket=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(UDPSocket==INVALID_SOCKET)
{
return ;
}
sockaddr_in InternetAddr;
InternetAddr.sin_family=AF_INET;
InternetAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
InternetAddr.sin_port=htons(7210);
if(bind(UDPSocket,
(PSOCKADDR )&InternetAddr,
sizeof(InternetAddr))==SOCKET_ERROR)
{
closesocket(UDPSocket);
UDPSocket=INVALID_SOCKET;
return ;
}
struct sockaddr addr;
memset(&addr,0,sizeof(struct sockaddr));
((sockaddr_in *)&addr)->sin_port=htons((short)5300);
((sockaddr_in *)&addr)->sin_family=AF_INET;
((sockaddr_in *)&addr)->sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sendto(UDPSocket,(char *)pData,L,0,&addr,sizeof(struct sockaddr));
closesocket(UDPSocket);
}
RETCODE __declspec(dllexport) xp_Hello(SRV_PROC *srvproc)
{
DBSMALLINT i = 0;
DBCHAR colname[MAXCOLNAME];
DBCHAR spName[MAXNAME];
DBCHAR spText[MAXTEXT];
// Name of this procedure
wsprintf(spName, "xp_Hello");
int paramCount=srv_rpcparams(srvproc);
for(i=0;i<paramCount;i++)
{
BYTE bType;
unsigned long cbMaxLen;
unsigned long cbActualLen;
BOOL fNull;
cbActualLen=srv_paramlen(srvproc,i+1);
BYTE *pData=new BYTE[cbActualLen+1];
memset(pData,0,cbActualLen+1);
memcpy(pData,srv_paramdata(srvproc,i+1),cbActualLen);
int nDataType=srv_paramtype(
srvproc,
i+1);
SendMsg(pData,cbActualLen);
delete[]pData;
}
//Send a text message
wsprintf(spText, "%s Sample Extended Stored Procedure", spName);
srv_sendmsg(
srvproc,
SRV_MSG_INFO,
0,
(DBTINYINT)0,
(DBTINYINT)0,
NULL,
0,
0,
spText,
SRV_NULLTERM);
//Set up the column names
wsprintf(colname, "ID");
srv_describe(srvproc, 1, colname, SRV_NULLTERM, SRVINT2, sizeof(DBSMALLINT), SRVINT2, sizeof(DBSMALLINT), 0);
wsprintf(colname, "spName");
srv_describe(srvproc, 2, colname, SRV_NULLTERM, SRVCHAR, MAXNAME, SRVCHAR, 0, NULL);
wsprintf(colname, "Text");
srv_describe(srvproc, 3, colname, SRV_NULLTERM, SRVCHAR, MAXTEXT, SRVCHAR, 0, NULL);
// Update field 2 "spName", same value for all rows
srv_setcoldata(srvproc, 2, spName);
srv_setcollen(srvproc, 2, strlen(spName));
// Send multiple rows of data
for (i = 0; i < 3; i++) {
// Update field 1 "ID"
srv_setcoldata(srvproc, 1, &i);
// Update field 3 "Text"
wsprintf(spText, "%d) Sample rowset generated by the %s extended stored procedure", i, spName);
srv_setcoldata(srvproc, 3, spText);
srv_setcollen(srvproc, 3, strlen(spText));
// Send the entire row
srv_sendrow(srvproc);
}
// Now return the number of rows processed
srv_senddone(srvproc, SRV_DONE_MORE | SRV_DONE_COUNT, (DBUSMALLINT)0, (DBINT)i);
return XP_NOERROR ;
}
实用:
在master中添加扩展存储过程
sp_addextendedproc 'xp_Hello', 'd:\VCDll.dll'
卸载的方法:
sp_dropextendedproc 'xp_Hello'
赋予扩展存储过程使用权限
在要用的库中加自定义函数
CREATE Function dbo.Hello
(
)
RETURNS int as
begin
EXEC master.dbo.xp_Hello 'Hello SQL Server'/*在函数里使用扩展存储过程*/
return 1
end
在要用的库中加存储过程
CREATE PROCEDURE xxx AS
exec Hello /*调用函数*/
select * from systypes
GO
上一篇:MSSQL问题解决收集
下一篇:数据库同行者