c++ 在sql查询语句里怎么使用字符串变量
代码如下:#include"stdafx.h"#include<icrsint.h>#include<iostream>#include<iomanip>#include...
代码如下:
#include"stdafx.h"
#include <icrsint.h>
#include<iostream>
#include<iomanip>
#include <string>
using namespace std;
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
int main()
{
//连接数据库
CoInitialize(NULL); //初始化COM库环境
_ConnectionPtr Sp_JiBenFaData;
HRESULT hr = Sp_JiBenFaData.CreateInstance(_uuidof(Connection));
if (FAILED(hr)){
cout << "_ConnectionPtr对象指针1实例化失败!!!" << endl;
}
else{
try {
_bstr_t conn_JiBenFaData = "Driver={sql server};server=9.72.5.222;uid=sa;pwd=clicfjdata;database=JiBenFaData;";
Sp_JiBenFaData->Open(conn_JiBenFaData, "", "", adModeUnknown);
}
catch (_com_error &e) {
cout << e.Description() << endl;
}
_RecordsetPtr m_Risk;
if (FAILED(m_Risk.CreateInstance(_uuidof(Recordset)))){
cout << "记录集对象指针实例化失败!" << endl;
}
CString a = "001";
CString strSQL;
strSQL.Format(_T("select * from dbo.Risk where TeamId = '%s'"), a);
wchar_t *pSQL = strSQL.AllocSysString();
m_Risk->Open(pSQL, Sp_JiBenFaData.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText);
m_Risk->MoveFirst();
while (!m_Risk->adoEOF){
string RiskId = (char*)(_bstr_t)(m_Risk->Fields->GetItem(_variant_t("RiskId"))->Value);
cout << RiskId << endl;
m_Risk->MoveNext();
}
}
system("pause");
return 0;
}
编译通过,但跳出对话框报错:
Unhandled exception at 0x75E0C42D in test1.exe: Microsoft C++ exception: _com_error at memory location 0x0021F71C. 展开
#include"stdafx.h"
#include <icrsint.h>
#include<iostream>
#include<iomanip>
#include <string>
using namespace std;
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
int main()
{
//连接数据库
CoInitialize(NULL); //初始化COM库环境
_ConnectionPtr Sp_JiBenFaData;
HRESULT hr = Sp_JiBenFaData.CreateInstance(_uuidof(Connection));
if (FAILED(hr)){
cout << "_ConnectionPtr对象指针1实例化失败!!!" << endl;
}
else{
try {
_bstr_t conn_JiBenFaData = "Driver={sql server};server=9.72.5.222;uid=sa;pwd=clicfjdata;database=JiBenFaData;";
Sp_JiBenFaData->Open(conn_JiBenFaData, "", "", adModeUnknown);
}
catch (_com_error &e) {
cout << e.Description() << endl;
}
_RecordsetPtr m_Risk;
if (FAILED(m_Risk.CreateInstance(_uuidof(Recordset)))){
cout << "记录集对象指针实例化失败!" << endl;
}
CString a = "001";
CString strSQL;
strSQL.Format(_T("select * from dbo.Risk where TeamId = '%s'"), a);
wchar_t *pSQL = strSQL.AllocSysString();
m_Risk->Open(pSQL, Sp_JiBenFaData.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText);
m_Risk->MoveFirst();
while (!m_Risk->adoEOF){
string RiskId = (char*)(_bstr_t)(m_Risk->Fields->GetItem(_variant_t("RiskId"))->Value);
cout << RiskId << endl;
m_Risk->MoveNext();
}
}
system("pause");
return 0;
}
编译通过,但跳出对话框报错:
Unhandled exception at 0x75E0C42D in test1.exe: Microsoft C++ exception: _com_error at memory location 0x0021F71C. 展开
1个回答
展开全部
string RiskId = (char*)(_bstr_t)(m_Risk->Fields->GetItem(_variant_t("RiskId"))->Value);//先判断m_Risk->Fields->GetItem(_variant_t("RiskId"))是否是无效值,再获取Value
追问
应该不是这个问题,因为如果变量是整型,程序没错,可以输出。
int a = 321;
strSQL.Format(_T("select * from dbo.Risk where id = %d"), a);
两行改成这样,其他不变,是没错的。
追答
数据库直接执行select * from dbo.Risk where TeamId = '001'对比下,你的teamid是字符串,自己跟踪到哪句错误?m_Risk->Open还是while循环里?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询