我想删除符合某字段=='7'的记录集,
这样写老出错strField ="f_field1";
m_strFilter="7";
m_pRecordset->Filter = strField + " = '" + m_strFilter + "'";
m_pRecordset->Open(_variant_t(strSQL),
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
m_pRecordset->Delete(adAffectGroup);
m_pRecordset->Update();应该怎么写啊?求高手指点。
这样写老出错strField ="f_field1";
m_strFilter="7";
m_pRecordset->Filter = strField + " = '" + m_strFilter + "'";
m_pRecordset->Open(_variant_t(strSQL),
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
m_pRecordset->Delete(adAffectGroup);
m_pRecordset->Update();应该怎么写啊?求高手指点。
直接在 SQL 语句中的 WHERE 语句中加入条件实现肯定没有问题。int i = 7;
CString strSQL;strSQL.Format("DELETE YourTable WHERE YourColumn = %d",i);因为删除操作不需要返回值,你用 Command 对象直接执行就可以了。
CString str="7"; //是字符串,不是数值strSQL.Format("DELETE Table WHERE f_field1 = str);m_pRecordset->Open(_variant_t(strSQL),
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);这样就行了?locktype应该用哪个?
UNSPECIFIED -1
READONLY 1
PESSIMISTIC 2
OPTIMISTIC 3
BATCHOPTIMISTIC 4
多谢咯
另外,你的 f_field1 列是数值型还是字符型?还有,你所说的错误是编译时的错误?还是运行时的错误?
字符串就这样:CString str = "7"; //是字符串,不是数值strSQL.Format("DELETE YourTable WHERE YourColumn = '%s'",str);另外,象 Insert、Update 和 Delete 这种 SQL 语句,你可以用 ADO 中的 Command 对象执行,因为这些语句不需要得到记录集,只要能执行就可以了。如果你要求得到记录集,你就用 Recordset 对象。 如果你没用过 Command 对象,你可以在网上查查怎么用,应该比 Recordset 还要简单。