Re: c++ ado
From: alex (alex.smotritsky_at_verizon.net)
Date: 05/30/04
- Next message: alex: "Re: Canceling Ado Connection"
- Previous message: David Bernheim: "Re: Using VFP OLE DB within VB6"
- In reply to: rc: "Re: c++ ado"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 30 May 2004 17:15:39 -0400
"rc" <rc@rc.com> wrote in message
news:qwmuc.4665$Yd3.1962@newsread3.news.atl.earthlink.net...
> Please post the code that you are using.
> "yagiroy" <alex.smotritsky@verizon.net> wrote in message
> news:67835095.0405300457.3bff039e@posting.google.com...
> > i need to connect to sql server with a db connection string specified
> > at runtime in my console app. i'm only able to connect when i hard
> > code the string or assign the hard coded string to a variable and use
> > it in the app. I'm not able to connect with strings read in from the
> > command line. I've been playing around with string conversion and
> > stuff for about 2 days and now i'm looking for an example of this that
> > works because i'm done playing around. i'm about ready to go to .net
> > if i can't find an example of how to do this with unmanaged code.
>
>
I'll paste the code below. Currenty the app will ignore command line args
and connect to sql server using a string var to which i have assigned a hard
coded db connect string. Code that gets the connect string from console
input is commented out or ignored. The app displays a list of databases on
the server. The only modification i will make to the code is making the user
id and password bla and bla. Most of the code is commented out or unused
because it's part of my failed attempt to connect using command line
arguments.
Here it is, thanks for looking.
// dbgrowth.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
rename("EOF", "EndOfFile")
#include <stdio.h>
#include <ole2.h>
#include <conio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
//#include <string.h>
using namespace std;
//Function Declaration.
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void DbGrowth(void);
void TableHeader(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);
void main()
{
if(FAILED(::CoInitialize(NULL)))
return;
DbGrowth();
::CoUninitialize();
}
void DbGrowth(void)
{
string targetConnectString, repositoryConnectString, tmpStr;
//char * t, r;
cout << "enter target connection string: ";
getline(cin, targetConnectString);
//char a_string[100];
_bstr_t bstrOne = targetConnectString.c_str();
////////////////////////////
_bstr_t myBstr;
//string myStr = ...
if ( ! targetConnectString.empty() )
{
wchar_t *buffer = new wchar_t [targetConnectString.size()+1];
if ( (size_t)-1 !=
mbstowcs(buffer,targetConnectString.c_str(),targetConnectString.size()+1))
myBstr = buffer;
delete[] buffer;
}
else
{
// Conversion error
}
//}
////////////////////////////
//getline(cin,a_string);
const char * t = targetConnectString.c_str();
//const bstr = targetConnectString.c_str();
BSTR BVAL = _bstr_t(targetConnectString.c_str()).copy();
//const int STRLEN = targetConnectString.length();
char ConnectString[200] = { 0 };
strcpy(ConnectString, targetConnectString.c_str());
cout << "ConnectString: " << ConnectString << '\n';
/*
The proper method to return a BSTR* is
*pVal = _bstr_t("Something").copy();
*/
//const char * t = targetConnectString.c_str;
/*
CString to char pointer
CString MyString = "ABCDEF";
char * szMyString = (char *) (LPCTSTR) MyString;
*/
//_bstr_t target2 = (LPCSTR)targetConnectString.c_str();
//cout << '\n' << "target2: " << target2;
cout << '\n' << "enter repository connection string: ";
getline(cin, repositoryConnectString);
cout << '\n';
// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_RecordsetPtr pRstDbs = NULL;
// Define Other Variables
HRESULT hr = S_OK;
_variant_t index;
index.vt = VT_I2;
// Assign connection string to a variable.
// const char *s
cout << "targetConnectString.c_str(): " << targetConnectString.c_str() <<
'\n';
string dasfk = "Provider='sqloledb';Data Source='OKSANA\\OKSANA';Initial
Catalog='pubs';Integrated Security='SSPI';User ID=bla;Password=bla";
//dasfk = targetConnectString.c_str();
const _bstr_t strCnn(dasfk.c_str()); //works when dasfk was assigned hard
coded connect string.
//_bstr_t strCnn(bstrOne.copy());
//const _bstr_t strCnn(ConnectString);
//const bstr_t strCnn(targetConnectString.c_str());
//bstr_t strCnn = targetConnectString.c_str();
//works
//bstr_t strCnn = "Provider='sqloledb';Data Source='OKSANA\\OKSANA';Initial
Catalog='pubs';Integrated Security='SSPI';User ID=bla;Password=bla";
//_bstr_t strCnn(myBstr);
//bstr_t
// cout << "_T(targetConnectString.c_str()): " << targetConnectString <<
'\n';
//_bstr_t strCnn("Provider='sqloledb';Data Source='OKSANA\\OKSANA';Initial
Catalog='pubs';Integrated Security='SSPI';User ID=bla;Password=bla");
try
{
// Open recordset
TESTHR(pRstDbs.CreateInstance(__uuidof(Recordset)));
// Pass the Cursor type and Lock type to the Recordset.
// get all the databases on the server
//cout << "trying to connect with: " << strCnn. << '\n';
pRstDbs->Open(
"SET nocount ON select name,crdate,version from master..sysdatabases"
, strCnn, adOpenForwardOnly,
adLockReadOnly, adCmdText);
// loop thru all the dbs on the server
if (!(pRstDbs==NULL))
{
TableHeader();
int intLine = 0;
while(!pRstDbs->EndOfFile)
{
index.iVal = 0;
cout.width(20);
cout << left << (LPCSTR)(_bstr_t)pRstDbs->GetFields()->GetItem(&
index)->Value << '\n';
pRstDbs->MoveNext();
}
}
cout << '\n' << "display done";
cin >> tmpStr;
}
catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstDbs->GetActiveConnection();
// GetActiveConnection returns connect string if connection
// is not open, else returns Connection object.
switch(vtConnect.vt)
{
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
cout << '\n' << "error";
cin >> tmpStr;
}
// Clean up objects before exit.
if (pRstDbs)
if (pRstDbs->State == adStateOpen)
pRstDbs->Close();
getch();
}
void TableHeader(void)
{
cout.width(20);
cout << left << "Databases";
cout << '\n' << "--------------------" << '\n';
}
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);
printf("\t Error number: %x\t%s", pErr->Number,
(LPCSTR) pErr->Description);
}
}
}
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print Com errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
// EndNextRecordsetCpp
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
- Next message: alex: "Re: Canceling Ado Connection"
- Previous message: David Bernheim: "Re: Using VFP OLE DB within VB6"
- In reply to: rc: "Re: c++ ado"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|