CDatabase connection;
CString connstr("DSN=MyODBC");
TCHAR strsql[1024];
_snprintf(strsql, 1204, "select pid, name, pwd from probe where pid = ?");     // ???????????????????????try
{
    if (connection.OpenEx(connstr), CDatabase::openReadOnly | CDatabase::noOdbcDialog)
    {
CRecordset rs(&connection);
if (rs.Open(AFX_DB_USE_DEFAULT_TYPE, strsql))
{
    while (!rs.IsEOF())
    {
                // ......
rs.MoveNext();
    }
    rs.Close();
}
     connection.Close();
    }
}
catch (CDBException* e)
{
    printf(e->m_strError);
}上面标上问号的一行用了where pid = ?,请问这种预处理的语句应该怎样帮定参数?
请教,多谢!!!!!!

解决方案 »

  1.   

    如果你数据库里面的pid字段是整数型.则:
    int i_id=你要的数字;
    _snprintf(strsql, 1204, "select pid, name, pwd from probe where pid = %d",i_id); 如果是字符型.则:
    int i_id=你要的数字;
    _snprintf(strsql, 1204, "select pid, name, pwd from probe where pid = '%d'",i_id); 
      

  2.   

    问题是有的字段不是int,而是varchar类型的,楼上这么写就不对了,
    还是需要用预处理语句多谢!
      

  3.   

    如果是varchar就用第二种情况的语句嘛~ 加上单引号~~