_variant_t RecordsAffected;
_ConnectionPtr m_pConnection;CString strSQL; strSQL.Format("INSERT INTO my_user(id,username) VALUES('111', 'shgh')",&RecordsAffected,adCmdText);m_pConnection->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);
_ConnectionPtr m_pConnection;CString strSQL; strSQL.Format("INSERT INTO my_user(id,username) VALUES('111', 'shgh')",&RecordsAffected,adCmdText);m_pConnection->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);
strSQL.Format("INSERT INTO my_user(id,username) VALUES(\'%d\', \'%s\')",RecordsAffected.llVal,adCmdText);
strSQL="INSERT INTO my_user(id,username) values('111','abc');";
即可,如果id字段在数据库中是整形的,那么values里的111不用带单引号,不放心的话在执行SQL语句前可以用
AfxMessageBox(strSQL)打印看看你的SQL语句是否正确。
strSQL.Format("INSERT INTO my_user(id,username) VALUES('111', 'shgh')“;
//或者是下面这句:
strSQL.Format("INSERT INTO my_user(id,username) VALUES(%d, '%s')",&RecordsAffected,adCmdText);
_ConnectionPtr m_pConnection; CString strSQL; strSQL="INSERT INTO my_user(id,username) values('111','abc');"; m_pConnection->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);
是这样吗???还是不行..
SQL Code_variant_t RecordsAffected;
_ConnectionPtr m_pConnection; CString strSQL; strSQL="INSERT INTO my_user(id,username) values('111','abc')"; m_pConnection->Execute( _bstr_t(strSQL),NULL,adCmdText);
几点注意:
1.保证你当前使用的database是目标数据库
2.connection已经打开!
3.目标table存在
4.多用catch (){}看错误!catch (_com_error e)
{
TRACE0("catch _com_error!\n");
//m_pADOConn.Release();
CString strComError;
strComError.Format("Error: %08lx\nError Message: %s\nSource: %s\nDescription: %s",
e.Error(), // ´íÎó±àºÅ
e.ErrorMessage(), // ´íÎóÐÅÏ¢
(LPCSTR) e.Source(), // ´íÎóÔ´
(LPCSTR) e.Description()); // ´íÎóÃèÊö
MessageBox(strComError,"Database Error",MB_ICONEXCLAMATION);
this->SendMessage(WM_CLOSE);
return FALSE;
}
这样写:
try
{
_variant_t RecordsAffected;
_ConnectionPtr m_pConnection; CString strSQL; strSQL="INSERT INTO my_user(id,username) values(111,'abc');"; m_pConnection->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);
}
catch(_com_error &e)
{
AfxMessageBox(e.Description());
}
这是什么意思?字符串格式化有这样的写法?
唉,这样写得了
strSQL.Format("INSERT INTO my_user(id,username) VALUES('%s', '%s')",strid,strusername);