我写了一段数据库查询程序,实现已知性别和学号查找姓名,
我想将要查找的数据库中的字段以外部参数形式传给strSql,代码如下:
其中BtnYZ()函数中的各参数已有值如下:
m_1=0405121;
ss="name";,sss="性别";
void CGRDPJSDlg::BtnYZ(int m_1,CString ss,CString sss)
{
CoInitialize(NULL);
try
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strSql;
strSql.Format("select canshu='%s' from class_table where sex='%s' and id=%f",ss,sss,m_1);
BSTR bstrSQL=strSql.AllocSysString();
m_pRecordset->Open(bstrSQL,(IDispatch *)m_pConnection,adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
_variant_t TheValue;
TheValue=m_pRecordset->GetCollect("ss");
ohd=(char*)_bstr_t(TheValue); //ohd已定义
}
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
m_pRecordset->Close();
CoUninitialize();
}
请高手指点,怎样才能实现所查询的字段可从外部输入?
我想将要查找的数据库中的字段以外部参数形式传给strSql,代码如下:
其中BtnYZ()函数中的各参数已有值如下:
m_1=0405121;
ss="name";,sss="性别";
void CGRDPJSDlg::BtnYZ(int m_1,CString ss,CString sss)
{
CoInitialize(NULL);
try
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strSql;
strSql.Format("select canshu='%s' from class_table where sex='%s' and id=%f",ss,sss,m_1);
BSTR bstrSQL=strSql.AllocSysString();
m_pRecordset->Open(bstrSQL,(IDispatch *)m_pConnection,adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
_variant_t TheValue;
TheValue=m_pRecordset->GetCollect("ss");
ohd=(char*)_bstr_t(TheValue); //ohd已定义
}
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
m_pRecordset->Close();
CoUninitialize();
}
请高手指点,怎样才能实现所查询的字段可从外部输入?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货