-
Visual C++ and ADO connections to Sql Server
Hi,
I have this program written in Visual C++ and I am having difficulty connecting
it to Sql Server through an ADO connection. Does anyone have any suggestions?
Thanks.
-
Re: Visual C++ and ADO connections to Sql Server
"Maria" <marsmith7@home.com> wrote:
>
>Hi,
>I have this program written in Visual C++ and I am having difficulty connecting
>it to Sql Server through an ADO connection. Does anyone have any suggestions?
>Thanks.
since you did not post your code....here is an example i have that uses the
#import method of adding ADO functionality to a vc++ app.
//----------------------------------------------------------------------------
// Goal: To connect to the named database and perform a simple query.
void CADOImportDlg::OnDbConnection()
{
HRESULT hr = S_OK;
ADODB::_RecordsetPtr Rs1 = NULL;
ADODB::_ConnectionPtr pConn = NULL;
_variant_t varAcct, varTicker, varPosition;
CString szConnStr = "Provider=SQLOLEDB;Initial Catalog=(DB name);";
szConnStr += "Data Source=PCMIKEM;User ID=(userID);Password=(pwd);";
CString szQuery = "SELECT Account, Ticker, sum(Position) TotalPosition
";
szQuery += "FROM TickerList WHERE Account = 'COHE' GROUP BY Account, Ticker
";
_bstr_t Connect(szConnStr);
_bstr_t Source (szQuery);
hr = pConn.CreateInstance(__uuidof(ADODB::Connection));
if (FAILED(hr))
{ TRACE("Error: Failed to create a Connection instance");
return;
}
pConn->Open(Connect, L"vartrackuser", L"vartrackuser", -1);
hr = Rs1.CreateInstance( __uuidof( ADODB::Recordset ) );
if (FAILED(hr))
{ TRACE("Error: Failed to create a recordset instance");
return;
}
Rs1->Open( Source, pConn.GetInterfacePtr(),
ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, -
1 );
//Loop through the RecordSet and output the Ticker to the
//CListBox
while (!Rs1->GetadoEOF())
{ varAcct = Rs1->GetCollect(_variant_t("Account"));
varTicker = Rs1->GetCollect(_variant_t("Ticker"));
varPosition = Rs1->GetCollect(_variant_t("TotalPosition"));
//Change each type to BSTR
varAcct.ChangeType(VT_BSTR);
varTicker.ChangeType(VT_BSTR);
varPosition.ChangeType(VT_BSTR);
CString szAcct = varAcct.bstrVal;
CString szTicker = varTicker.bstrVal;
CString szPosition = varPosition.bstrVal;
CString szOutput;
szOutput.Format("%-10s%10s%20s", szAcct, szTicker, szPosition);
m_lstOutput.AddString(szOutput);
Rs1->MoveNext();
}
Rs1->Close();
Rs1 = NULL;
::MessageBox( NULL, "Success!", "", MB_OK );
}
Note, you should add this statement to the stdafx.h file
//---------------------------------------------
// Import Type Libraries
#import <msado15.dll> rename( "EOF", "adoEOF" )
Also, search MSDN there are some good articles on this topic.
cheers,
mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks