65,186
社区成员




CoInitialize(NULL);
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
//_Recordset m_recordset;
HRESULT hr;
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
CString strSQL = "insert into TABLE1(idd,a) values('1','2')";
CString str_conn="driver={SQL Server};Provider=MSDASQL.1;Password=a;Persist Security Info=True;User ID=sa;Data Source=user";
_bstr_t bstrSRC(str_conn);
_variant_t varSQL(strSQL);
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open(bstrSRC,"","",adModeUnknown);
AfxMessageBox("c");
m_pRecordset->Open(strSQL,m_pConnection,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic);
//连接数据库
m_pRecordset->AddNew();
AfxMessageBox("d");
m_pRecordset->put_Collect(_variant_t((CString)"idd"),_variant_t((CString)"12"));
m_pRecordset->put_Collect(_variant_t("a"),_variant_t("9"));
m_pRecordset->Update();
AfxMessageBox("bbc");
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
m_pConnection.Release();
CoUninitialize();