日期:2014-05-18  浏览次数:20581 次

DEBUG ERROR。。很郁闷。。请赐教
//我在"stdafx.h"里添加了<afx.h>,并注释掉了<windows.h>,请大牛注意
//pConn->Execute("INSERT INTO "+asd+"(ID,username,old,birthday) VALUES(1,"+asd+",25,'1970/1/1')",NULL,adCmdText);(这句DEBUGERROR)




#include "stdafx.h"
#include <string>

using namespace std;
string tablename="xcvb";
_bstr_t asd =tablename.c_str();
//_bstr_t fgh=asd;
CString tablenamee("zzz");
BSTR bstrText = tablenamee.AllocSysString();
//SysFreeString(bstrText);

int APIENTRY WinMain(HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  LPSTR lpCmdLine,
  int nCmdShow)
{
  // TODO: Place code here.
// TODO: Add your control notification handler code here


CoInitialize(NULL);

_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
_CommandPtr pCmd(__uuidof(Command));
try
{

//pConn->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=pubs";
pConn->ConnectionString="Provider=SQLOLEDB.1;Password=forebase;Persist Security Info=True;User ID=sa;Initial Catalog=shts;Data Source=127.0.0.1";
pConn->Open("","","",adConnectUnspecified);
}
  catch(_com_error e) //捕捉异常
  {
   
  return FALSE;
  }

  try
{
pRst->Open(_variant_t("xcvb"),
_variant_t((IDispatch *)pConn,true),  
adOpenKeyset,adLockOptimistic,  
adCmdTable);
  pConn->Execute("INSERT INTO "+asd+"(ID,username,old,birthday) VALUES (1,"+asd+",25,'1970/1/1')",NULL,adCmdText);
}
catch (_com_error &e)
{
pConn->Execute("CREATE TABLE "+asd+"(ID INTEGER,username VARCHAR,old INTEGER,birthday DATETIME)",NULL,adCmdText);
   
  pConn->Execute("INSERT INTO "+asd+"(ID,username,old,birthday) VALUES (1,'Washington',25,'1970/1/1')",NULL,adCmdText);
}

pRst->Close();
pConn->Close();
pCmd.Release();
pRst.Release();
pConn.Release();
CoUninitialize();

return 0;

 
 


------解决方案--------------------
SQL code

--Execute('你少了单引号')
--eg:exec('select * from tb')