51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1861|回复: 0
打印 上一主题 下一主题

游戏编程系统3----数据库

[复制链接]
  • TA的每日心情
    慵懒
    2015-1-8 08:46
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2007-11-23 14:48:59 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
    windows下面 做游戏,还是老老实实用mssqlserver吧.最新的sqlserver 2005性能提高很多了.
    而且有了SQL Server Management Studio.爽.

    介绍一个SQLOLEDB通过存储过程的实现吧.代码实际上是我用韩国***游戏服务端改出来了.

    和SQL连接的方式很多,OLEDB是我觉得功能最强大的.


    定义基类
    COLECore

    class COLECore
    {
    public:
    COLECore();
    ~COLECore();

    HRESULT     SDKGetDataSource();
    HRESULT     InitConnection(char* ip, char* db, char* user, char* pass);
    bool     Clear();

    HRESULT     ResultProc();
    HRESULT     SetCommandText(ICommandText* pICommandText, WCHAR* wCmdString);
    HRESULT     Execute();
    HRESULT     TestFunction();

    IDBCreateCommand*  GetCreateCommand() { return m_pIDBCreateCommand; }

    HRESULT     InitParameters();

    IDBInitialize*   m_pIDBInitialize;
    IDBProperties*   m_pIDBProperties;
    IDBCreateSession*  m_pIDBCreateSession;
    IDBCreateCommand*  m_pIDBCreateCommand;
    ITransactionLocal*  m_pITransaction;
    ICommandText*   m_pICommandText;
    IRowset*    m_pIRowset;
    IColumnsInfo*   m_pIColumnsInfo;
    DBCOLUMNINFO*   m_pDBColumnInfo;
    IAccessor*    m_pIAccessor;
    DBPROPSET    m_rgInitPropSet[1];
    ULONG     m_i, m_j;
    LONG     m_cNumRows;
    ULONG     m_lNumCols;
    WCHAR*     m_pStringsBuffer;
    DBBINDING*    m_pBindings;
    ULONG     m_ConsumerBufColOffset;
    HACCESSOR    m_hAccessor;
    HROW     m_hRows[10];
    HROW*     m_pRows;
    BYTE*     m_pBuffer;
    };

    实现
    CLSID CLSID_SQLOLEDB = {0xc7ff16cL,0x38e3,0x11d0,{0x97,0xab,0x0,0xc0,0x4f,0xc2,0xad,0x98}};

    COLECore::COLECore()
    {
    m_cNumRows = 0;
    m_ConsumerBufColOffset = 0;
    m_pRows = &m_hRows[0];
    m_pIAccessor = NULL;
    m_pICommandText = NULL;
    m_pIDBCreateCommand = NULL;
    m_pIDBCreateSession = NULL;
    m_pIDBInitialize = NULL;
    }

    COLECore::~COLECore()
    {
    }

    HRESULT COLECore::SDKGetDataSource()
    {
    return S_OK;
    }

    HRESULT COLECore::InitConnection(char* ip, char* db, char* user, char* pass)
    {
    HRESULT hr;
    int i = 0;

    CoInitialize(NULL);

    hr = CoCreateInstance(
      CLSID_SQLOLEDB,
      NULL,
      CLSCTX_INPROC_SERVER,
      IID_IDBInitialize,
      (void**)&m_pIDBInitialize);

    if (FAILED(hr))
    {
      return E_FAIL;
    }

    DBPROP InitProperties[4];

    for (i = 0; i < 4; i++)
    {
      VariantInit(&InitProperties.vValue);
    }

    USES_CONVERSION;

    // Server Name;
    InitProperties[0].colid    = DB_NULLID;
    InitProperties[0].dwOptions   = DBPROPOPTIONS_REQUIRED;
    InitProperties[0].dwPropertyID  = DBPROP_INIT_DATASOURCE;
    InitProperties[0].dwStatus   = 0;
    InitProperties[0].vValue.vt   = VT_BSTR;
    InitProperties[0].vValue.bstrVal = ::SysAllocString(A2COLE(ip));

    // Database Name;
    InitProperties[1].colid    = DB_NULLID;
    InitProperties[1].dwOptions   = DBPROPOPTIONS_REQUIRED;
    InitProperties[1].dwPropertyID  = DBPROP_INIT_CATALOG;
    InitProperties[1].dwStatus   = 0;
    InitProperties[1].vValue.vt   = VT_BSTR;
    InitProperties[1].vValue.bstrVal = ::SysAllocString(A2COLE(db));

    // User Name;
    InitProperties[2].colid    = DB_NULLID;
    InitProperties[2].dwOptions   = DBPROPOPTIONS_REQUIRED;
    InitProperties[2].dwPropertyID  = DBPROP_AUTH_USERID;
    InitProperties[2].dwStatus   = 0;
    InitProperties[2].vValue.vt   = VT_BSTR;
    InitProperties[2].vValue.bstrVal = ::SysAllocString(A2COLE(user));

    // Password
    InitProperties[3].colid    = DB_NULLID;
    InitProperties[3].dwOptions   = DBPROPOPTIONS_REQUIRED;
    InitProperties[3].dwPropertyID  = DBPROP_AUTH_PASSWORD;
    InitProperties[3].dwStatus   = 0;
    InitProperties[3].vValue.vt   = VT_BSTR;
    InitProperties[3].vValue.bstrVal = ::SysAllocString(A2COLE(pass));

    m_rgInitPropSet[0].guidPropertySet = DBPROPSET_DBINIT;
    m_rgInitPropSet[0].cProperties  = 4;
    m_rgInitPropSet[0].rgProperties  = InitProperties;

    hr = m_pIDBInitialize->QueryInterface(
      IID_IDBProperties,
      (void**)&m_pIDBProperties);

    if (FAILED(hr))
    {
      return E_FAIL;
    }

    hr = m_pIDBProperties->SetProperties(1, m_rgInitPropSet);

    if (FAILED(hr))
    {
      return E_FAIL;
    }
    m_pIDBProperties->Release();

    if (FAILED(m_pIDBInitialize->Initialize()))
    {
      return E_FAIL;
    }

    // Create Session
    if (FAILED(m_pIDBInitialize->QueryInterface(
      IID_IDBCreateSession,
      (void**)&m_pIDBCreateSession)))
    {
      return E_FAIL;
    }

    if (FAILED(m_pIDBCreateSession->CreateSession(
      NULL,
      IID_IDBCreateCommand,
      (IUnknown**)&m_pIDBCreateCommand)))
    {
      return E_FAIL;
    }

    if (FAILED(m_pIDBCreateCommand->CreateCommand(
      NULL,
      IID_ICommandText,
      (IUnknown**)&m_pICommandText)))
    {
      return E_FAIL;
    }

    return S_OK;
    }

    HRESULT COLECore::SetCommandText(ICommandText* pICommandText, WCHAR* wCmdString)
    {
    if (pICommandText == NULL)
    {
      return E_FAIL;
    }

    if (FAILED(pICommandText->SetCommandText(DBGUID_DBSQL, wCmdString)))
    {
      return E_FAIL;
    }

    return S_OK;
    }

    HRESULT COLECore::Execute()
    {

    if (FAILED(m_pICommandText->Execute(
      NULL,
      IID_IRowset,
      NULL,
      &m_cNumRows,
      (IUnknown**)&m_pIRowset)))
    {
      return E_FAIL;
    }

    return S_OK;
    }

    HRESULT COLECore::ResultProc()
    {
    m_ConsumerBufColOffset = 0;
    HRESULT hr;
    if (m_pIRowset == NULL)
    {
      return E_FAIL;
    }

    hr = m_pIRowset->QueryInterface(
      IID_IColumnsInfo,
      (void**)&m_pIColumnsInfo);

    if (FAILED(hr))
    {
      return E_FAIL;
    }

    m_pIColumnsInfo->GetColumnInfo(
      &m_lNumCols,
      &m_pDBColumnInfo,
      &m_pStringsBuffer);

    m_pIColumnsInfo->Release();

    return S_OK;
    }

    bool COLECore::Clear()
    {
    if (m_pIAccessor != NULL)
    {
      m_pIAccessor->Release();
    }

    if (m_pICommandText != NULL)
    {
      m_pICommandText->Release();
    }

    if (m_pIDBCreateCommand != NULL)
    {
      m_pIDBCreateCommand->Release();
    }

    if (m_pIDBCreateSession != NULL)
    {
      m_pIDBCreateSession->Release();
    }

    if (m_pIDBInitialize != NULL)
    {
      if (FAILED(m_pIDBInitialize->Uninitialize()))
      {
       return false;
      }
      m_pIDBInitialize->Release();
    }

    CoUninitialize();

    return true;
    }

    HRESULT COLECore::InitParameters()
    {
    return S_OK;
    }


    定义COLEWorker类和子类COLEInput,COLEOutput
    #define MAX_CMD_STR    256
    class COLEWorker
    {
    public:
    COLEWorker();
    ~COLEWorker();

    WCHAR   m_wCmdString[MAX_CMD_STR];

    ICommandText* m_pICommandText;
    IAccessor*  m_pIAccessor;
    IRowset*  m_pIRowset;

    COLECore*  m_pCore;
    HACCESSOR  m_hAccessor;

    HRESULT   Init(COLECore* pCore, WCHAR* wCmdString);

    virtual HRESULT CreateAccessor() { return S_OK; }
    virtual void Release();
    };

    class COLEInput : public COLEWorker
    {
    public:
    COLEInput();
    ~COLEInput();
    };

    class COLEOutput : public COLEWorker
    {
    public:
    HACCESSOR m_hRowAccessor;

    COLEOutput();
    ~COLEOutput();

    virtual void Release();
    };

    实现
    COLEWorker::COLEWorker()
    {
    //
    }

    COLEWorker::~COLEWorker()
    {
    //
    }

    void COLEWorker::Release()
    {
    if (m_pIAccessor != NULL)
    {
      m_pIAccessor->ReleaseAccessor(m_hAccessor, NULL);
      m_pIAccessor->Release();
    }

    if (m_pIRowset != NULL)
    {
      m_pIRowset->Release();
    }
    }

    HRESULT COLEWorker::Init(COLECore* pCore, WCHAR* wCmdString)
    {
    if (pCore == NULL)
    {
      return E_FAIL;
    }

    wcscpy_s(m_wCmdString, MAX_CMD_STR, wCmdString);
    m_pCore = pCore;

    if (FAILED((pCore->GetCreateCommand())->CreateCommand(
      NULL,
      IID_ICommandText,
      (IUnknown**)&m_pICommandText)))
    {
      return E_FAIL;
    }

    if (S_OK != m_pCore->SetCommandText(m_pICommandText, wCmdString))
    {
      return E_FAIL;
    }

    return CreateAccessor();
    }

    COLEInput::COLEInput()
    {
    COLEWorker::COLEWorker();
    }

    COLEInput::~COLEInput()
    {
    COLEWorker::~COLEWorker();
    }

    COLEOutput::COLEOutput()
    {
    COLEWorker::COLEWorker();
    }

    COLEOutput::~COLEOutput()
    {
    COLEWorker::~COLEWorker();
    }

    void COLEOutput::Release()
    {
    if (m_pIAccessor != NULL)
    {
      m_pIAccessor->ReleaseAccessor(m_hAccessor, NULL);
      m_pIAccessor->ReleaseAccessor(m_hRowAccessor, NULL);
      m_pIAccessor->Release();
    }

    if (m_pIRowset != NULL)
    {
      m_pIRowset->Release();
    }
    }

    完蛋,我发现我不善言辞,代码也少有说明.
    我发现连我自己都不知道我自己讲的是什么...唉.失败...

    小二,上代码!
    看不懂也别怪我了...我的语言水平实在有限
    不过这个确实好用哈.
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-23 00:34 , Processed in 0.073853 second(s), 28 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表