ado 数据库

USE MK_UboxChs
GO
/****** Object:  StoredProcedure [dbo].[Login]    Script Date: 08/05/2010 10:33:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: SQLQuery11.sql|7|0|E:/Documents and Settings/wangbaoming.CN-MIC/Local Settings/Temp/~vs620F.sql
-- =============================================
-- Author:        <Author,,Name>
-- Create date: <Create Date,,>
-- Description:    <Description,,>
-- =============================================
create PROCEDURE [dbo].[Login4](
    -- Add the parameters for the stored procedure here
  @loginName nchar(20),
  @passWord  int,
  @empId     int out)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT * from  TOHOSPITAL_TBL where TH_PATIENT_ID < @passWord
    --return @@rowcount
END

 

 

 

 

USE [MK_UboxChs]
GO
/****** Object:  StoredProcedure [dbo].[Login45]    Script Date: 03/10/2011 17:11:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: SQLQuery11.sql|7|0|E:/Documents and Settings/wangbaoming.CN-MIC/Local Settings/Temp/~vs620F.sql
-- =============================================
-- Author:        <Author,,Name>
-- Create date: <Create Date,,>
-- Description:    <Description,,>
-- =============================================
create PROCEDURE [dbo].[Login5](
    -- Add the parameters for the stored procedure here
  @loginName nchar(20),
  @passWord  int,
  @empId     int out)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT @empId=TOHOSPITAL_ID from  TOHOSPITAL_TBL where TH_PATIENT_ID = @passWord
   
    return @@rowcount
END

 

// db.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "db.h"
#include "WXDBConnect.h"
#include <vector>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 唯一的应用程序对象

CWinApp theApp;

using namespace std;



int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    CoInitialize(NULL);

    // 初始化 MFC 并在失败时显示错误
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: 更改错误代码以符合您的需要
        _tprintf(_T("错误: MFC 初始化失败/n"));
        nRetCode = 1;
    }
    else
    {
        CWXDBConnect db;
        db.SetSQLConcetText(("Provider=SQLOLEDB; Server=7BWZ82X_win7pro//uboxc;Database=MK_UboxChs; uid=sa; pwd=U-BOXc1921#;"));
        if(!db.Open())
        {
            std::cout<< "open error" << endl;
        }

        std::vector<_variant_t> vecVarParam;
        vecVarParam.push_back(_variant_t("sss"));
        vecVarParam.push_back(_variant_t(2));


        std::vector<_variant_t> vecVarOutParam;
        _variant_t ret = db.ExecStoredProc("Login5",vecVarParam, vecVarOutParam);

        cout << ret.intVal << endl;
        cout << (char *)((_bstr_t)(vecVarOutParam.at(0).bstrVal)) << endl;
        cout << vecVarOutParam.at(1).intVal << endl;
        cout << vecVarOutParam.at(2).intVal << endl;

           
        _RecordsetPtr rec = db.ExecStoredProc("Login4",vecVarParam);
        while(1)
        {
            cout << rec->GetCollect("TH_PATIENT_ID").intVal << endl;
            if(FAILED(rec->MoveNext()))
            {
                break;
            }
        }

        //vector<_variant_t> t;

        //for(vector<_variant_t>::iterator iter=t.begin();iter!=t.end();iter++)
        //{
        //    string str((char *)((_bstr_t)(*iter)));
        //    cout<< str.c_str() << endl;
        //}

    }


    CoUninitialize();

    return nRetCode;
}

 

 

WXDBConnect.h

 

#pragma once

#import "C:/Program Files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF")
#define _WX_COMM_ADO_
#include "../wxcomm.h"
#include <string>
#include <vector>

class CWXDBConnect
{
public:
    CWXDBConnect(void);
    ~CWXDBConnect(void);

    //打开数据库
    bool Open();

    //执行SQL
    _RecordsetPtr ExecText(std::string strText);

    //调用存储过程 参数中仅有输入参数
    _RecordsetPtr ExecStoredProc(std::string strStoredProc,
        const std::vector<_variant_t>& vecVarParam) throw(...);
    //调用存储过程 参数中有输出参数
    _variant_t ExecStoredProc(std::string strStoredProc,
        const std::vector<_variant_t>& vecVarParam,
        std::vector<_variant_t>& vecVarOutParam) throw(...);

    //关闭数据库
    bool Close();

private:
    _ConnectionPtr m_pConnection;
    _CommandPtr m_pCommandPtr;
    _RecordsetPtr m_pRecordset;

    //"Provider=SQLOLEDB; Server=127.0.0.1//uboxc;Database=MK_UboxChs; uid=user; pwd=****;"
    WX_COMM_ADD_ATTRIBUTE_0(std::string, SQLConcetText, str);

    //设定输入参数
    void SetCmdParam(const std::vector<_variant_t>& vecVarParam) throw(...);
};

 

WXDBConnect.cpp

 

#include "StdAfx.h"
#include "WXDBConnect.h"

CWXDBConnect::CWXDBConnect(void)
{
    //初始化Connection指针
    m_pConnection.CreateInstance(_uuidof(Connection));
    //初始化m_pCommandPtr指针
    m_pCommandPtr.CreateInstance(_uuidof(Command));
    //初始化Recordset指针
    m_pRecordset.CreateInstance(_uuidof(Recordset));

}

CWXDBConnect::~CWXDBConnect(void)
{
    m_pConnection.Release();
    m_pCommandPtr.Release();
    m_pRecordset.Release();
}

bool CWXDBConnect::Open()
{
    HRESULT hr;
    try
    {
        hr = m_pConnection->Open(this->m_strSQLConcetText.c_str(),"","",NULL);
    }
    catch (...)
    {
        return false;
    }

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

    return true;
}

bool CWXDBConnect::Close()
{   
    HRESULT hr;
    try
    {
        hr = m_pConnection->Close();
    }
    catch (...)
    {
        return false;
    }

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

    return true;
}


_RecordsetPtr  CWXDBConnect::ExecText(std::string strText)
{
    m_pCommandPtr->ActiveConnection = m_pConnection; 
    m_pCommandPtr->CommandText = strText.c_str(); 
    m_pCommandPtr->CommandType = adCmdText; 

    return m_pCommandPtr->Execute(NULL,NULL,adCmdUnknown); 
}

void CWXDBConnect::SetCmdParam(const std::vector<_variant_t>& vecVarParam) throw(...)
{
    //刷新存储过程的参数
    m_pCommandPtr->Parameters->Refresh();
    //输入参数个数大于存储过程的参数个数时,抛出异常
    if(vecVarParam.size() > (unsigned long)(m_pCommandPtr->Parameters->Count-1))
    {
        throw("param count error");
    }   

    //设定参数
    for(unsigned long i=0; i<vecVarParam.size(); ++i)
    {
        m_pCommandPtr->Parameters->GetItem((long)(i+1))->Value = vecVarParam.at(i);
    }
}

_RecordsetPtr  CWXDBConnect::ExecStoredProc(std::string strStoredProcName,
                                            const std::vector<_variant_t>& vecVarParam) throw(...)
{
    m_pCommandPtr->ActiveConnection = m_pConnection; 
    m_pCommandPtr->CommandText = _bstr_t(strStoredProcName.c_str()); 
    m_pCommandPtr->CommandType = adCmdStoredProc; 

    //设定输入参数
    this->SetCmdParam(vecVarParam);

    return m_pCommandPtr->Execute(NULL,NULL,adCmdStoredProc); 
}

_variant_t CWXDBConnect::ExecStoredProc(std::string strStoredProc,
                                   const std::vector<_variant_t>& vecVarParam,
                                   std::vector<_variant_t>& vecVarOutParam) throw(...)
{
    //调用存储过程
    this->ExecStoredProc(strStoredProc, vecVarParam);

    //保存参数
    for(long i=1; i<m_pCommandPtr->Parameters->Count; ++i)
    {
        vecVarOutParam.push_back(m_pCommandPtr->Parameters->GetItem(i)->Value);
    }

    //返回值
    _variant_t varRet;
    if(m_pCommandPtr->Parameters->Count>0)
    {
        varRet = m_pCommandPtr->Parameters->GetItem((long)(0))->Value;
    }
    else
    {
        varRet.Clear();
    }

    return varRet;
}

你可能感兴趣的:(Date,vector,String,null,Parameters,login)