Article View: microsoft.public.data.odbc
Article #1SQLConfigDataSource: trying to remove a DSN
From: Paul Maia
Date: Sun, 14 Apr 2002 23:28
Date: Sun, 14 Apr 2002 23:28
109 lines
5168 bytes
5168 bytes
I am trying roll my own un-installer. Yep, I know... There are
a lot of tools out there for this, but I want to do it myself.
What better way to learn! After 25 years in this industry, I
still want to learn... Things were so simple with Turbo Pascal
1.0 on DOS, in the 70's!
I am running C++ Builder 5.0, with all patches. Windows 2000
is the dev environment, test environment is Windows 2000 and
Windows 98.
I have ACCESS database connectivity in my app. I know, SQL
would be better, but I have ACCESS and it works (or maybe it
doesn't) so... hmm... OK.
I have included the code that I am using... I have cut some
pieces to make the code more readable and less verbose.
Here's the problem... See CreateTest() below. The creation of
the DSN works as prescribed. The DSN is created. I have confirmed
this in the ODBC utility, in the Control Panel.
When I call (see below) RemoveTest(), I get an error 11,
ODBC_ERROR_REQUEST_FAILED, a generic error that indicates
that the underlying ConfigDSN() has failed. Hmm... Not a lot of
help, there.
I have tried this on Windows 2000 and Windows 98 SE. I have also
tried adding all of the attributes (applicable ones, anyway) that
I used in the Create function, but to no avail...
I am using ODBC 3.5xxx on both systems.
Have I done something stupid? Or is there something I just don't
understand? Hmm... Both I suspect... Any clues???
Now the code....
---------------------------------------------------------------
BOOL Create_MSAccess_DSN(char *dsn, char *path, char *db_name,
char *description)
{
char szAttributes[MAX_PATH * 2 + 200];
int retCode = 0;
DWORD errNum;
char msg[SQL_MAX_MESSAGE_LENGTH];
// Build attribute string
sprintf(szAttributes, "DSN=%s;FIL=MS Access;JetIniPath=odbcddp.ini;"
"DBQ=%s;DefaultDir=%s;Description=%s;UID=;PWD=;;", dsn,
db_name, path, description);
// Now replace the semi's with Nulls per the spec
CreateAttributeString(szAttributes);
// go create it
retCode = SQLConfigDataSource(NULL, ODBC_ADD_SYS_DSN, driver,
szAttributes);
// any error?
if (retCode == 0)
{
memset(msg, 0, sizeof(msg));
SQLInstallerError(1, &errNum, msg, sizeof(msg), NULL);
}
// did it work? 0 is failure
return(retCode);
}
//---------------------------------------------------------------------------
// deletes/removes a Microsoft Access ODBC DSN
// returns >0 on success or 0 on failure
// NOTE: db_name needs to be a fully qualified path to the DB
BOOL Remove_MSAccess_DSN(char *dsn, char *path, char *db_name)
{
char szAttributes[200]; // a DSN, and some extra
int retCode;
DWORD errNum;
char msg[SQL_MAX_MESSAGE_LENGTH];
// Build attribute string
sprintf(szAttributes, "DSN=%s;;", dsn);
// Now replace the semi's with Nulls per the spec
CreateAttributeString(szAttributes);
// go create it
retCode = SQLConfigDataSource(Application->Handle,
ODBC_REMOVE_DSN, driver, szAttributes);
// any error?
if (retCode == 0)
{
memset(msg, 0, sizeof(msg));
SQLInstallerError(1, &errNum, msg, sizeof(msg), NULL);
}
// did it work? 0 is failure
return(retCode);
}
void CreateTest()
{
if (Create_MSAccess_DSN("Fred", "D:\\Databases", "D:\\Databases\\Data.MDB",
"Database For Repeater-Decoder Monitor"))
MessageBox(NULL, "Returned TRUE", "Create_MSAccess_DSN", MB_OK);
else
MessageBox(NULL, "Returned FALSE", "Create_MSAccess_DSN", MB_OK);
}
void RemoveTest()
{
if (Remove_MSAccess_DSN("Fred", "D:\\Databases", "D:\\Databases\\Data.MDB"))
MessageBox(NULL, "Returned TRUE", "Remove_MSAccess_DSN", MB_OK);
else
MessageBox(NULL, "Returned FALSE", "Remove_MSAccess_DSN", MB_OK);
}
Technical Details
Message-ID:
<fe095dc7.0204141528.444ead6f@posting.google.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10288e,98eefc741c09f0e6,start X-Google-Attributes: gid10288e,public X-Google-Thread: fc5c9,98eefc741c09f0e6,start X-Google-Attributes: gidfc5c9,public X-Google-Thread: fcacf,98eefc741c09f0e6,start X-Google-Attributes: gidfcacf,public X-Google-Thread: ff0ac,98eefc741c09f0e6,start X-Google-Attributes: gidff0ac,public X-Google-Thread: fc5f9,98eefc741c09f0e6,start X-Google-Attributes: gidfc5f9,public X-Google-ArrivalTime: 2002-04-14 16:28:39 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail From: paul@fn-Software.com (Paul Maia) Newsgroups: microsoft.public.data.odbc,comp.databases,comp.databases.ms-access,borland.public.cppbuilder.database,borland.public.cppbuilder.database.sqlservers Subject: SQLConfigDataSource: trying to remove a DSN Date: 14 Apr 2002 16:28:39 -0700 Organization: http://groups.google.com/ Lines: 109 Message-ID: <fe095dc7.0204141528.444ead6f@posting.google.com> NNTP-Posting-Host: 24.28.83.102 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1018826919 17400 127.0.0.1 (14 Apr 2002 23:28:39 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 14 Apr 2002 23:28:39 GMT