SQL执行结果@@ROWCOUNT怎样在mfc工程中读出?代码如下:if((select count(*) from mydb.dbo.testTB where name='test')=0)
insert into mydb.dbo.testTB (name, memo) VALUES('test','测试数据')select @@ROWCOUNT tempcnt sqlquery ="if((select count(*) from mydb.dbo.testTB where name='test')=0) insert into mydb.dbo.testTB (name, memo) VALUES('test','测试数据') select @@ROWCOUNT tempcnt"; try
{
m_pRecordset = m_pConnection->Execute(sqlquery,&RecordsAffected,adCmdText); while(!m_pRecordset->ADOEOF)
{
temprow=m_pRecordset->GetCollect("totalCnt");
temprow.ChangeType(VT_I4);
int uValue = temprow.lVal;
m_pRecordset->MoveNext();
}
//m_pRecordset->Close();
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format(_T("插入失败!请重新点击按钮“输入数据库”!\r\n错误信息:%s 错误描述:%s"),e.ErrorMessage(),(LPCSTR)e.Description());
AfxMessageBox(errormessage);
return ;
}
结果打断点看到的结果如下(uValue的值)
请问这是哪里写错了?
insert into mydb.dbo.testTB (name, memo) VALUES('test','测试数据')select @@ROWCOUNT tempcnt sqlquery ="if((select count(*) from mydb.dbo.testTB where name='test')=0) insert into mydb.dbo.testTB (name, memo) VALUES('test','测试数据') select @@ROWCOUNT tempcnt"; try
{
m_pRecordset = m_pConnection->Execute(sqlquery,&RecordsAffected,adCmdText); while(!m_pRecordset->ADOEOF)
{
temprow=m_pRecordset->GetCollect("totalCnt");
temprow.ChangeType(VT_I4);
int uValue = temprow.lVal;
m_pRecordset->MoveNext();
}
//m_pRecordset->Close();
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format(_T("插入失败!请重新点击按钮“输入数据库”!\r\n错误信息:%s 错误描述:%s"),e.ErrorMessage(),(LPCSTR)e.Description());
AfxMessageBox(errormessage);
return ;
}
结果打断点看到的结果如下(uValue的值)
请问这是哪里写错了?
{
nValue = 0;
try
{
_variant_t vtFld = m_ptrRecordset->Fields->GetItem(lpcsrFieldName)->Value;
switch(vtFld.vt)
{
case VT_BOOL:
{
nValue = vtFld.boolVal;
break;
}
case VT_I2:
case VT_UI1:
{
nValue = vtFld.iVal;
break;
}
case VT_NULL:
case VT_EMPTY:
{
nValue = 0;
break;
}
default: nValue = vtFld.iVal;
}
return true;
}
catch(_com_error& comError)
{
RecordErrorMsg(comError);
} return false;
}
加VT_I4进去,
enum VARENUM
{ VT_EMPTY = 0,
VT_NULL = 1,
VT_I2 = 2,
VT_I4 = 3,
VT_R4 = 4,
VT_R8 = 5,
VT_CY = 6,
VT_DATE = 7,
VT_BSTR = 8,
VT_DISPATCH = 9,
VT_ERROR = 10,
VT_BOOL = 11,
VT_VARIANT = 12,
VT_UNKNOWN = 13,
VT_DECIMAL = 14,
VT_I1 = 16,
VT_UI1 = 17,
VT_UI2 = 18,
VT_UI4 = 19,
VT_I8 = 20,
VT_UI8 = 21,
VT_INT = 22,
VT_UINT = 23,
VT_VOID = 24,
VT_HRESULT = 25,
VT_PTR = 26,
VT_SAFEARRAY = 27,
VT_CARRAY = 28,
VT_USERDEFINED = 29,
VT_LPSTR = 30,
VT_LPWSTR = 31,
VT_RECORD = 36,
VT_INT_PTR = 37,
VT_UINT_PTR = 38,
VT_FILETIME = 64,
VT_BLOB = 65,
VT_STREAM = 66,
VT_STORAGE = 67,
VT_STREAMED_OBJECT = 68,
VT_STORED_OBJECT = 69,
VT_BLOB_OBJECT = 70,
VT_CF = 71,
VT_CLSID = 72,
VT_VERSIONED_STREAM = 73,
VT_BSTR_BLOB = 0xfff,
VT_VECTOR = 0x1000,
VT_ARRAY = 0x2000,
VT_BYREF = 0x4000,
VT_RESERVED = 0x8000,
VT_ILLEGAL = 0xffff,
VT_ILLEGALMASKED = 0xfff,
VT_TYPEMASK = 0xfff
} ;
不好意思,刚接触这方面的知识,不太懂你的意思,可以麻烦你再解释一下吗?针对小白的解释,谢谢啦
RecordsAffected等价于SQL中的@@ROWCOUNT,不需要特地执行select @@ROWCOUNT
谢谢,我明白了,问题是@@ROWCOUNT返回的值不对,在sql server里查询的话返回的@@ROWCOUNT是对的,但是在mfc里返回的值经常是错的(有时候正确),用下面的代码读的,
while(!m_pRecordset->ADOEOF)
{
count=m_pRecordset->GetCollect("cnt");
m_pRecordset->MoveNext();
}
insertCnt=_ttoi((LPCTSTR)(_bstr_t)count);
这是什么原因呀?