您的位置:首页 > 编程语言 > C语言/C++

c++调用ado执行带参数的sql(非存储过程)

2015-11-07 23:17 423 查看
///////////////////////////////////////////////
//      PrintProviderError Function          //
///////////////////////////////////////////////

void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr  pErr = NULL;

if( (pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
TRACE(_T("\tError number: %x\t%s"), pErr->Number,
(LPCTSTR)pErr->Description);
}
}
}

//////////////////////////////////////
//      PrintComError Function      //
//////////////////////////////////////

void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());

// Print Com errors.
TRACE(_T("Error\n"));
TRACE(_T("\tCode = %08lx\n"), e.Error());
TRACE(_T("\tErrorMessage = %s\n"), (LPCTSTR)e.ErrorMessage());
TRACE(_T("\tSource = %s\n"), (LPCTSTR) bstrSource);
TRACE(_T("\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}

///////////////////////////////////////////////
//      ExecuteSQL Function          //
///////////////////////////////////////////////
void ExecuteSQL()
{
CoInitialize(NULL);
HRESULT    hr = S_OK;

// Define string variables.
_bstr_t strCnn("Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=Participants;Data Source=(local)");

// Define ADO object pointers.
// Initialize pointers on define.
_ConnectionPtr  pConnection = NULL;
_CommandPtr     pCmd  = NULL;
_RecordsetPtr   pRst  = NULL;
_ParameterPtr   pParam = NULL;

try
{
// Open connection.
pConnection.CreateInstance(__uuidof(Connection));
pConnection->Open (strCnn, "", "", adConnectUnspecified);

// Please note that question mark, a question mark represents a parameter
_bstr_t strSQL("update p1 set phonenumber='3344' where id=?;");

// Create command object.
pCmd.CreateInstance(__uuidof(Command));
pCmd->ActiveConnection = pConnection;
pCmd->CommandText = strSQL;
pCmd->CommandType = adCmdText;

// Append some parameters
// According to statements to maintain the order and type parameters.
// In this case, the parameter names can be customized.
pCmd->Parameters->Append(pCmd->CreateParameter(_T("@id"),adVarChar, adParamInput,(long)10, _T("ID002")));

_variant_t vRecordAffected;
vRecordAffected.vt = VT_I4;
vRecordAffected.lVal = 0L;

pCmd->Execute(vRecordAffected.GetAddress(),NULL,adCmdText);

TRACE(_T("nRecordAffected=%d\r\n"),  vRecordAffected.lVal);

// Clean up objects before exit.

pCmd.Release();

if (pRst)
{
if (pRst->State == adStateOpen)
pRst->Close();
pRst.Release();
}

if (pConnection)
{
if (pConnection->State == adStateOpen)
pConnection->Close();
pConnection.Release();
}
}
catch (_com_error &e)
{
PrintProviderError(pConnection);
PrintComError(e);
}
::CoUninitialize();
}

c#我们知道sql语句中的参数,用“@param”来代替

但是在c++中,用“?”问号代替

_bstr_t strSQL("update p1 set phonenumber='3344' where id=?;")中,参数用?号来代替

再创建参数

  pCmd->CreateParameter(_T("@id"),adVarChar, adParamInput,(long)10, _T("ID002"))

其中“@id”只起到可读性作用,并不会根据此标识,自动绑定到sql相应位置,

也就是说,参数的添加

pCmd->Parameters->Append,要按sql中参数的顺序来添加,否则会乱了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: