Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OLE DB:VC++->MSSQL2K

Status
Not open for further replies.

michaelkrauklis

Programmer
Dec 5, 2001
226
US
I've got a question concerning OLE DB. Is there an easy way to connect to a data source using OLE DB? I've done it using ODBC and the CDatabase object but as far as I know the CDatabase object only supoprts ODBC, not OLE DB. Is there something like that which can allow you to easily use OLE DB? All the tutorials I find take me through the MFC Wizard which is completely useless to me. I don't need something terribly complicated. I don't need overhead, and as of yet I don't even really need a GUI. I just need to insert data into a Microsoft SQL2k server from VC++ and from what I've read OLE DB is faster than ODBC, which is what I'm using now. Any help will be greatly appreciated. Thanks! MYenigmaSELF:-9
myenigmaself@yahoo.com
 
Hi

Assuming that the database is created in SQL and that you gave the correct access right to a user, here is the code

// Set Db Connection String

m_strConnection.Format(
"Provider=sqloledb;Server=%s;Database=%s;Trusted_Connection=Yes;", m_strSQLServer, m_strDbName);

// Open Db

try
{
m_pConnection->Open(( _bstr_t) m_strConnection, _bstr_t(L""), _bstr_t(L""), adModeUnknown);
}
catch( _com_error& e)
{
AfxADOErrorMessageBox( "Error Opening Database (" + m_strDbName + "):", e);

return FALSE;
}

I assume that you declare the following as a global object and that you have created the instance:

_ConnectionPtr m_pConnection;

The most important function is the following:

void AfxADOErrorMessageBox( CString strContext, _com_error& e)
{

// Extract Error Source and Description

_bstr_t bstrSource( e.Source());
_bstr_t bstrDescription( e.Description());

CString str;

str.Format( "%s: ADO Exception Code %08lx \n - Meaning: %s \n - Source: %s \n - Description: %s",
strContext, e.Error(), e.ErrorMessage(), ( LPCTSTR) bstrSource, ( LPCTSTR) bstrDescription);

AfxMessageBox( str, MB_OK);
}

It really help to knwo what happens

HTH

Thierry
EMail: Thierry.Marneffe@swing.be
 
Hi

here is the content of stdafx.h

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#include <afxinet.h> // MFC Internet Support

// Include ADO Engine
#import &quot;c:\program files\common files\system\ado\msado15.dll&quot; no_namespace rename (&quot;EOF&quot;, &quot;adoEOF&quot;)

This allows the use of smart pointers for Db access

HTH

Thierry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top