c++通过ADO对数据库操作-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

c++通过ADO对数据库操作

C/C++ 通过ADO对数据库操作

成都创新互联公司总部坐落于成都市区,致力网站建设服务有网站建设、网站制作、网络营销策划、网页设计、网站维护、公众号搭建、小程序设计、软件开发等为企业提供一整套的信息化建设解决方案。创造真正意义上的网站建设,为互联网品牌在互动行销领域创造价值而不懈努力!

实例:c++ 通过ADO调用存储过程写入数据到sql server

1.在.h头文件导入微软提供的ado的动态库

#pragma warning(disable:4146)

#import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" named_guids rename("EOF","adoEOF"), rename("BOF","adoBOF")

#pragma warning(default:4146)

using namespace ADODB;

2.编写函数调用存储过程

//保存数据库记录

void SaveDbTran(char *devID,char * traceNo,char *refNo,char *batchNo, char * bankCardNo, char * tranDate, char * tranTime,char * amt,char * hosCardNo,char * hosName,char * hosIDCardNo, char *msg)

{

    //2.1创建数据库连接句柄

    _ConnectionPtr pMyConnect; 

HRESULT hr;

    

   

    try                 

    {    

   //2.1.1组建连接数据库字符串

char *dbConStr = new char[400];

strcpy(dbConStr,"Provider=SQLOLEDB;Server=sqlserver服务器地址,1433;Database=数据库名;uid=登陆用户;pwd=登录密码");

CoInitialize(NULL);

     

   Sleep(300);

//2.1.2 句柄实例化 实现连接

hr = pMyConnect.CreateInstance("ADODB.Connection");

if(SUCCEEDED(hr))

{

ct.SaveLog("创建数据库连接对象成功,等待打开连接",0,dbConStr);

pMyConnect->Open(_bstr_t(dbConStr),"","",adModeUnknown); 

}

   else

{

CoUninitialize();

return;

}  

    }

    catch(_com_error &err)

    {

   

CoUninitialize();

return ;

    } 

   //2.2组建运行命令参数句柄

    _CommandPtr   m_pCommand;

_RecordsetPtr m_pRecordset; 

try

{

   //2.2.1 句柄实例化

m_pCommand.CreateInstance(__uuidof(Command));//Command无需改变

m_pCommand->ActiveConnection = pMyConnect;//pMyConnect 为连接数据库句柄

m_pCommand->CommandType = adCmdStoredProc;//adCmdStoredProc无需改变

m_pCommand->CommandText = _bstr_t("p_d_tra");//p_d_tra为存储过程名

       

//2.2.2 设置给可变参数赋值

_variant_t  vv_dotype,vv_TranNo,vv_devID,vv_TranDate,vv_TranTime;

        _variant_t  vv_RetCode,vv_BusType,vv_CardNo,vv_IDCard,vv_Name;

   _variant_t  vv_Amt,vv_Notes,vv_SerID,vv_DesCont,vv_tranFlag;

        _variant_t  vv_bankCardNo,vv_bankGroupNo,vv_bankFlowNo,vv_DevName,vv_ExeCount;

         // value to variant

        vv_dotype     =_variant_t(_bstr_t("1"));

        vv_TranNo     =_variant_t(_bstr_t(temp_traceNo));

vv_devID      =_variant_t(_bstr_t(devID));

vv_TranDate   =_variant_t(_bstr_t(datetime));

vv_TranTime   =_variant_t(_bstr_t(datetime));

vv_RetCode    =_variant_t(_bstr_t(""));

vv_BusType    =_variant_t(_bstr_t("14"));

vv_CardNo     =_variant_t(_bstr_t(hosCardNo));

vv_IDCard     =_variant_t(_bstr_t(hosIDCardNo));

vv_Name       =     _variant_t(_bstr_t(hosName));

vv_Amt        =_variant_t(_bstr_t(amt));

   vv_Notes      =_variant_t(_bstr_t("交易成功"));

vv_SerID  =_variant_t(_bstr_t(""));

        vv_DesCont    =_variant_t(_bstr_t(""));

vv_tranFlag   =_variant_t(_bstr_t("0"));

vv_bankCardNo =_variant_t(_bstr_t(temp_bankCardNo));

vv_bankGroupNo=_variant_t(_bstr_t(batchNo));

vv_bankFlowNo =_variant_t(_bstr_t(refNo));

        vv_DevName    =_variant_t(_bstr_t(""));

        vv_ExeCount   =     _variant_t(ret_ExeCount);

        

//2.2.3创建数据库存储过程输入参数

_ParameterPtr mp_dotype,mp_TranNo,mp_devID,mp_TranDate,mp_TranTime;

        _ParameterPtr mp_RetCode,mp_BusType,mp_CardNo,mp_IDCard,mp_Name;

   _ParameterPtr mp_Amt,mp_Notes,mp_SerID,mp_DesCont,mp_tranFlag;

    _ParameterPtr mp_bankCardNo,mp_bankGroupNo,mp_bankFlowNo,mp_DevName,mp_ExeCount;

//2.2.3.1给数据库参数实例化

        //create ParameterPtr instance

mp_dotype.CreateInstance(__uuidof(Parameter));

mp_TranNo.CreateInstance(__uuidof(Parameter));

mp_devID.CreateInstance(__uuidof(Parameter));

mp_TranDate.CreateInstance(__uuidof(Parameter));

mp_TranTime.CreateInstance(__uuidof(Parameter));

mp_RetCode.CreateInstance(__uuidof(Parameter));

mp_BusType.CreateInstance(__uuidof(Parameter));

mp_CardNo.CreateInstance(__uuidof(Parameter));

mp_IDCard.CreateInstance(__uuidof(Parameter));

mp_Name.CreateInstance(__uuidof(Parameter));

mp_Amt.CreateInstance(__uuidof(Parameter));

mp_Notes.CreateInstance(__uuidof(Parameter));

mp_SerID.CreateInstance(__uuidof(Parameter));

mp_DesCont.CreateInstance(__uuidof(Parameter));

mp_tranFlag.CreateInstance(__uuidof(Parameter));

mp_bankCardNo.CreateInstance(__uuidof(Parameter));

mp_bankGroupNo.CreateInstance(__uuidof(Parameter));

mp_bankFlowNo.CreateInstance(__uuidof(Parameter));

mp_DevName.CreateInstance(__uuidof(Parameter));

        mp_ExeCount.CreateInstance(__uuidof(Parameter));

         

        //2.2.3.2将数据库参数追加到 命令变量句柄的参数属性末尾

//append parameterPtr set  to _CommandPtr parameters lists 

//数据库参数 =  命令变量句柄->创建参数方法(_bstr_t("存储过程参数名"),存储过程参数数据类型,存储过程参数输入输出类型,默认大小,可变参数值)

        mp_dotype=m_pCommand->CreateParameter(_bstr_t("dotype"),adInteger,adParamInput,-1,vv_dotype);//整数类型

        m_pCommand->Parameters->Append(mp_dotype); 

mp_TranNo=m_pCommand->CreateParameter(_bstr_t("TranNo"),adVarChar,adParamInput,32,vv_TranNo); //字符串类型

        m_pCommand->Parameters->Append(mp_TranNo); 

mp_devID=m_pCommand->CreateParameter(_bstr_t("devID"),adVarChar,adParamInput,20,vv_devID);

        m_pCommand->Parameters->Append(mp_devID); 

        mp_TranDate=m_pCommand->CreateParameter(_bstr_t("TranDate"),adVarChar,adParamInput,25,vv_TranDate);//时间类型

        m_pCommand->Parameters->Append(mp_TranDate); 

mp_TranTime=m_pCommand->CreateParameter(_bstr_t("TranTime"),adVarChar,adParamInput,25,vv_TranTime);

        m_pCommand->Parameters->Append(mp_TranTime); 

mp_RetCode=m_pCommand->CreateParameter(_bstr_t("RetCode"),adVarChar,adParamInput,20,vv_RetCode);

        m_pCommand->Parameters->Append(mp_RetCode); 

        mp_BusType=m_pCommand->CreateParameter(_bstr_t("BusType"),adInteger,adParamInput,-1,vv_BusType);

        m_pCommand->Parameters->Append(mp_BusType); 

mp_CardNo=m_pCommand->CreateParameter(_bstr_t("CardNo"),adVarChar,adParamInput,20,vv_CardNo);

        m_pCommand->Parameters->Append(mp_CardNo); 

mp_IDCard=m_pCommand->CreateParameter(_bstr_t("IDCard"),adVarChar,adParamInput,20,vv_IDCard);

        m_pCommand->Parameters->Append(mp_IDCard); 

        mp_Name=m_pCommand->CreateParameter(_bstr_t("Name"),adVarChar,adParamInput,10,vv_Name);

        m_pCommand->Parameters->Append(mp_Name); 

mp_Amt=m_pCommand->CreateParameter(_bstr_t("Amt"),adDecimal,adParamInput,10,vv_Amt);//浮点数类型

mp_Amt->NumericScale = 2;  //设置小数点后位数

mp_Amt->Precision = 10;    //设置整数位位数

        m_pCommand->Parameters->Append(mp_Amt); 

mp_Notes=m_pCommand->CreateParameter(_bstr_t("Notes"),adVarChar,adParamInput,2000,vv_Notes);

        m_pCommand->Parameters->Append(mp_Notes); 

        mp_SerID=m_pCommand->CreateParameter(_bstr_t("SerID"),adVarChar,adParamInput,20,vv_SerID);

        m_pCommand->Parameters->Append(mp_SerID); 

mp_DesCont=m_pCommand->CreateParameter(_bstr_t("DesCont"),adVarChar,adParamInput,2000,vv_DesCont);

        m_pCommand->Parameters->Append(mp_DesCont); 

mp_tranFlag=m_pCommand->CreateParameter(_bstr_t("tranFlag"),adInteger,adParamInput,-1,vv_tranFlag);

        m_pCommand->Parameters->Append(mp_tranFlag); 

        

mp_bankCardNo=m_pCommand->CreateParameter(_bstr_t("bankCardNo"),adVarChar,adParamInput,25,vv_bankCardNo);

        m_pCommand->Parameters->Append(mp_bankCardNo); 

mp_bankGroupNo=m_pCommand->CreateParameter(_bstr_t("bankGroupNo"),adVarChar,adParamInput,12,vv_bankGroupNo);

        m_pCommand->Parameters->Append(mp_bankGroupNo); 

mp_bankFlowNo=m_pCommand->CreateParameter(_bstr_t("bankFlowNo"),adVarChar,adParamInput,12,vv_bankFlowNo);

        m_pCommand->Parameters->Append(mp_bankFlowNo); 

mp_DevName=m_pCommand->CreateParameter(_bstr_t("DevName"),adVarChar,adParamInput,40,vv_DevName);

        m_pCommand->Parameters->Append(mp_DevName); 

mp_ExeCount=m_pCommand->CreateParameter(_bstr_t("ExeCount"),adInteger,adParamOutput,-1,vv_ExeCount);//输出类型

        m_pCommand->Parameters->Append(mp_ExeCount); 

//2.2.4 执行得到结果

    _variant_t vNull;

vNull.vt = VT_ERROR;

vNull.scode = DISP_E_PARAMNOTFOUND;

//2.2.4.1执行数据库存储过程

m_pRecordset = m_pCommand->Execute(&vNull,&vNull,adCmdStoredProc);//adCmdStoredProc不要改变  

//m_pRecordset执行SQL结果 可对其进行判断确定成功,错误原因

      

    

if (mp_ExeCount->Value != 0) 

{

ct.SaveLog("插入数据失败",0,"");

}

else

{

            ct.SaveLog("插入数据成功",0,"");

}

}

catch(_com_error &err)

{

if(pMyConnect->State) pMyConnect->Close();

pMyConnect = NULL; 

CoUninitialize();

}

//2.2.5 关闭连接 释放资源

if(pMyConnect->State) pMyConnect->Close();

pMyConnect = NULL; 

CoUninitialize();  //切记成功与否都要调用 以释放资源

return ;

}


分享题目:c++通过ADO对数据库操作
URL链接:http://kswsj.cn/article/ieojdh.html

其他资讯