C++扩展存储过程编程--使用UDP发送数据库的更新通知

本文介绍了如何使用C++编写扩展存储过程,通过UDP发送数据库更新通知。当SQL Server中的数据发生变化时,触发器调用扩展存储过程,将更新信息以UDP形式传递给应用程序。详细步骤包括创建扩展存储过程工程、读取存储过程参数、构建UDP报文并发送、处理异常以及部署和测试存储过程。
摘要由CSDN通过智能技术生成

    关键字:VC++, SQL Server,UDP,扩展存储过程
   
    作者:邓杨均
    EMail: dyj057@gmail.com

    源代码: http://www.cnblogs.com/Files/dyj057/NetKingAlarmXP.rar
    
    当数据库更新的时候,如果插入,删除,修改了数据记录,我们的应用程序需要知道这
    些记录,就需要使用触发器,触发器再调用扩展存储过程,扩展存储过程再把数据使用
    UDP的通讯方式,发送到应用程序,这样应用程序就会收到数据库更新的通知。
   
    1. 创建扩展存储过程的工程
    启动Visual Studio 2003 -> 新建Visual C++项目 -> 扩展存储过程DLL
   
    2.在生成的向导中,输入你要创建的扩展存储过程的名称,这个很重要,一开始就要认真填写,
    否则工程创建完成后,就不好修改了.在我们例子里面就输入xp_door_web_site
   
    3.读取存储过程的参数,然后形成数据报文,再发送出去,该示例存储过程有4个参数.有varchar,int等数据类型.
    先来看看主要的处理流程:
    RETCODE __declspec(dllexport) xp_door_web_site(SRV_PROC *srvproc)                                        
    {                                                                                                        
     XPLib lib(srvproc); //Create tool class                                                                
                                                                                                             
     try                                                                                                    
     {                                                                                                      
      DWSEvent newEvent; // Create a new  event                                                       
      //Check paramerter's number                                                                          
      if (srv_rpcparams(srvproc) != 4)                                                                                                                                                  
      {                                                                                                    
       // Send usage message to client and return error                                                   
       lib.printUsage("xp_door_web_site <UserId>,<Url>,<Message>,<type>");                                
       return (XP_ERROR);                                                                                 
      }                                                                                                    
                                                                                                          
      lib.readVarcharPara(1,&newEvent.m_strUserId[0], (int)newEvent.m_strUserId.capacity(), false,"UserID");
      lib.readVarcharPara(2,&newEvent.m_strUrl[0], (int)newEvent.m_strUrl.capacity(), false,"Url");        
      lib.readVarcharPara(3,&newEvent.m_strMsg[0], (int)newEvent.m_strMsg.capacity(), false,"Msg");        
      lib.readIntPara(4,newEvent.m_nType, false,"Type");                                                   
                                                                                                          
      newEvent.SetConfigFile("C://XPdoorwebsite.config.ini");                                              
      newEvent.Cast();                                                                                     
     }                                                                                                      
     catch(CExceptionEx & me)                                                                               
     {                                                                                                      
      lib.printError( me.what() );                                                                         
      return XP_ERROR;                                                                                     
     }                                                                                                      
     catch(...)                                                                                             
     {                                                                                                      
      lib.printError("Unkowned exception occured");                                                        
      return XP_ERROR;                                                                                     
     }                                                                                                      
                                                                                                          
     return XP_NOERROR ;                                                                                    
    }
    代码都很容易理解,也很简单,就不多言  
   
    4. XPLib类库是我自己写的,是编写扩展存储过程主要的可复用的类,它的职能是读取存储过程的参数,和打印错误和使用信息.

    XPlib.h的声明如下:
    #pragma once                                                                                                          
                                                                                                                                                                                                                           
    #include <string>                                                                                                     
    using namespace std;                                                                                                  
                                                                                                                       
    namespace IBMS                                                                                                                                                                     
    {                                                                                                                                                                                                                  
    namespace Data                                                                                                        
    {                                                                                                                     
     class XPLib                                                                                                         
     {                                                                                                                   
     public:                                                                                                             
      XPLib(SRV_PROC *pSrvProc);                                                                                        
      ~XPLib(void);                                                                                                     
                                                                                                                      
      static const int MAX_SERVER_ERROR = 20000;                                                                        
      static const int XP_SEND_TO_IBMS_ERROR = MAX_SERVER_ERROR + 1;                                                    
                                                                                                                      
     public:                                                                                                             
      void printUsage (const char * usage);                                                                             
      void printError (const char * errroMsg);                                                                          
      void readIntPara( int nParaIndex, int &iValue, bool bAllowNull = true, string paraName="");                       
      void readVarcharPara(int nParaIndex, char * szValue, int nMaxLength=-1, bool bAllowNull = true,string paraName="");
                                                                                                                      
     private:                                                                                                            
      SRV_PROC * m_pSrvProc;                                                                                            
     };                                                                                                                  
    }                                                                                                                     
    }  
    XPlib.cpp的实现如下:
    #include "StdAfx.h"                                                                                                                                                                                                           
    #include "./xplib.h"                                                                                       
                                                                                                           
    #include <cassert>                                                                                         
    #include <sstream>                     

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值