Hi netters,
I am using VC++ 6, SQL 7, and the related SQL-DMO objects to manage
my database. During the first instalation of a program, it must
create its own database. Later instalations of the program should
recognize that the database already exists. I am just learning the
SQL-DMO, so that I would like to ask you whether my approach of
testing the existence of the database is correct or whether some
more elegant solution can be used.
Let's say that the database name, user name, and the server name are
defined as follow:
#define _SERVER "MyServer"
#define _USER "sa"
#define _PWD ""
#define _DATABASENAME "MyDatabase"
I start with #import of the SQL-DMO component.
#import "c:\mssql7\binn\resources\1033\sqldmo.rll"
using namespace SQLDMO; // for simplicity
Then I CoInitialize() and I do create the instance of SQLServer
object. After that I connec the SQLServer object to my SQL server
(the way how it is shown in the sample
C:\MSSQL7\DevTools\Samples\sqldmo\cpp\Smartptr\Smartptr.cpp). Now
the testing comes.
{
bool exists = false; // init -- pesimistic assumption
_DatabasePtr spDatabase;
try {
spDatabase = spSQLServer->GetDatabases()
->Item( _DATABASENAME, _USER );
exists = true; // if we are here then database exists
}
catch(_com_error pCE) {
// The error 00x80045510 says that the database exists.
if (pCE.Error() != 0x80045510) vDisplayError(pCE);
}
if (exists) {
printf("\nThe database " _DATABASENAME " exists.");
...could be removed here...
}
else {
printf("\nThe database " _DATABASENAME " does not exist.");
...do create it here...
}
}
And one more question. If the database exists, can it be removed by
the command spDatabase->Remove(); in the "...could be removed
here..." part of the source text? I am asking whether I do not
forget to do something more that possibly should be done (beginner).
Thanks for help,
Petr
--
Petr Prikryl, SKIL s.r.o., e-mail: skil(at)stbk.mcs.cz
Please, don't reply via e-mail as more people share the
address. Do reply via net news.