MFC 连接sql server

	HRESULT hr = S_FALSE;
	//Connection
	_ConnectionPtr m_spPdmConn;
	hr= m_spPdmConn.CreateInstance(__uuidof(Connection),NULL,CLSCTX_INPROC_SERVER);
	if(FAILED(hr))
	{
		AfxMessageBox(_T("数据库连接失败,请检查 msado 库是否完整!"));
		return ;
	}
	_CommandPtr pCmd;
	hr = pCmd.CreateInstance(__uuidof(Command),NULL,CLSCTX_INPROC_SERVER);
	if (FAILED(hr) || !pCmd)
	{
		return;
	}
	IClassFactoryPtr pCF;
	hr = ::CoGetClassObject(__uuidof(Recordset),CLSCTX_INPROC_SERVER,NULL,IID_IClassFactory,(void**)&pCF);
	if (FAILED(hr) || !pCF)
	{
		return;
	}

	CString strConnect;
	strConnect=L"Provider=SQLOLEDB.1;Persist Security Info=False;Data Source=127.0.0.1\CAXASQL;User ID=sa;Password=caxa;Initial Catalog=CAXAPK;";
	try
	{
		//orcl
		//strConnect.Format(_T("Provider=OraOLEDB.Oracle.1;Password=%s;Persist Security Info=True;User ID=system;Data Source=%s"),L"caxapk",L"orcl");
		
		hr = m_spPdmConn->Open((_bstr_t)strConnect, _bstr_t(_T("")), _bstr_t(_T("")), adModeUnknown);

		//pCmd->CommandText = sql;
		//pCmd->Execute(NULL,NULL,adCmdText | adExecuteNoRecords);
		//_RecordsetPtr pRecordSet;
		//HRESULT	hr = m_pCF->CreateInstance(NULL,__uuidof(_Recordset),(void**)&pRecordSet);
		//ASSERT(SUCCEEDED(hr) && pRecordSet);
		//pRecordSet->CursorLocation=adUseClient;

		//pRecordSet->Open(sql,m_pConnection.GetInterfacePtr(),
		//	adOpenStatic,adLockOptimistic,adCmdText);

		//return pRecordSet;

	}
	catch (_com_error e)
	{
		AfxMessageBox(e.Description());
		return ;
	}

 

你可能感兴趣的:(MFC)