///////////////////////////////////////////////////////////////////////////////
//
// Function: CreateDatabase
//
// Purpose: This function will create a new database.
//
// Arguments: svServerName - The name of the SQL server to run the script on
// svDatabaseName - The name of the SQL database to run the script on
// svDriver - ADO requires this, but for SQL server you just send in "SQL Server"
// svUserName - The system account for SQL server
// svUserPassword - The password for the system account
//
// Usage:
// if (CreateDatabase("SQLServer", "MyDatabase", "SQL Server", "sa", "saPassword") = FALSE) then
//
///////////////////////////////////////////////////////////////////////////////
function BOOL CreateDatabase(svServerName, svDatabaseName, svDriver, svUserName, svUserPassword)
STRING szADOCommObjID, szADOObjID, szConnSting;
OBJECT pADOObj, pADOCommObj;
begin
// Create ADO Connection Object to connect to the SQL server
szADOObjID = "ADODB.Connection";
set pADOObj = CreateObject(szADOObjID);
// Open the ADO Connection
pADOObj.Open(szConnSting);
// Create the ADO Command object to execute the script
szADOCommObjID = "ADODB.Command";
set pADOCommObj = CreateObject(szADOCommObjID);
pADOCommObj.ActiveConnection = pADOObj;
// Execute the call to create the database
pADOCommObj.CommandText = "Create database " + svDatabaseName;
pADOCommObj.Execute();
return TRUE;
end;
///////////////////////////////////////////////////////////////////////////////
//
// Function: ExecuteSQLScript
//
// Purpose: This function will execute a SQL script.
//
// Arguments: svServerName - The name of the SQL server to run the script on
// svDatabaseName - The name of the SQL database to run the script on
// svDriver - ADO requires this, but for SQL server you just send in "SQL Server"
// svUserName - The system account for SQL server
// svUserPassword - The password for the system account
// svScriptFile - A fully qualified path to a SQL script file
// svErrLog - Reference to a string which will contain teh log of errors occured during the execution of SQL script file
//
// Usage:
// ExecuteSQLScript("SQLServer", "MyDatabase", "SQL Server", "sa", "saPassword", "C:\Scripts\tables.sql", svLog)
//
// Notes: You could change this function to accept the script as a string
// instead of the file. With a file, you can add the scripts
// as files in the installation, putting them in a directory.
// This would allow them to be reused later, or they can just be
// deleted after each script is run.
//
///////////////////////////////////////////////////////////////////////////////
function BOOL ExecuteSQLScript(svServerName, svDatabaseName, svDriver, svUserName, svUserPassword, svScriptFile, svErrLog)
OBJECT pADOObj, pADOCommObj;
STRING szADOObjID, szADOCommObjID;
STRING svLine, szConnString, szSQL, svString;
STRING szHead, szTail;
NUMBER nResult, nRecords, nBegin, nEnd;
BOOL bExitLoop;
LIST listID;
LIST listCmd;
number nRet;
string szErrorLog;
begin
szErrorLog = "";
nRet = 0;
// Create an empty string list.
listID = ListCreate(STRINGLIST);
listCmd = ListCreate(STRINGLIST);
// Read the SQL script file into the list
if (ListReadFromFile(listID, svScriptFile) < 0) then // read list from file
svErrLog = "Unable to open SQL script: " + svScriptFile + ".";
return -1;
endif;
// Go through each list item and add it to a string (which will then hold the script)
szSQL = "";
nResult = ListGetFirstString(listID, svString);
while (nResult = 0)
if (StrFind(svString, "GO") = 0) then
// Remove comments
bExitLoop = FALSE;
repeat
nBegin = StrFind(szSQL, "/*");
nEnd = StrFind(szSQL, "*/");
if ((nBegin >= 0) && (nBegin < nEnd)) then
StrSub(szHead, szSQL, 0, nBegin);
StrSub(szTail, szSQL, nEnd+2, 65535);
szSQL = szHead + szTail;
else
bExitLoop = TRUE;
endif;
until (bExitLoop);
// Add it to commands
if (StrLengthChars(szSQL) > 0) then
ListAddString(listCmd, szSQL, AFTER);
endif;
szSQL = "";
elseif (StrFind(svString, "--") = 0) then
// It is a comment, so do nothing
else
szSQL = szSQL + "\r\n" + svString;
endif;
nResult = ListGetNextString(listID, svString);
endwhile;
// Be good and clean up your trash
ListDestroy(listID);
// Create ADO Connection Object to connect to the SQL server
szADOObjID = "ADODB.Connection";
set pADOObj = CreateObject(szADOObjID);
// Open the ADO Connection
pADOObj.Open(szConnString);
// Create the ADO Command object to execute the script
szADOCommObjID = "ADODB.Command";
set pADOCommObj = CreateObject(szADOCommObjID);
pADOCommObj.ActiveConnection = pADOObj;
// Execute each command
nResult = ListGetFirstString(listCmd, svString);
while (nResult = 0)
try
// Execute the call to run the script
pADOCommObj.CommandText = svString;
pADOCommObj.Execute(nRecords ,0x81);
catch
nRet = Err.Number;
///////////////////////////////////////////////////////////////////////////////
//
// Function: DoesDatabaseExist
//
// Purpose: This function will determine whether a given database exists.
//
// Arguments: svServerName - The name of the SQL server to run the script on
// svDatabaseName - The name of the SQL database to run the script on
// svDriver - ADO requires this, but for SQL server you just send in "SQL Server"
// svUserName - The system account for SQL server
// svUserPassword - The password for the system account
//
// Usage:
// if (DoesDatabaseExist("SQLServer", "MyDatabase", "SQL Server", "sa", "saPassword") = FALSE) then
//
///////////////////////////////////////////////////////////////////////////////
function BOOL DoesDatabaseExist(svServerName, svDatabaseName, svDriver, svUserName, svUserPassword)
OBJECT pADOConnObj, pADORecordSetObj;
STRING szADOConnObjID, szADORecordSetObjID, szConnString, szSQL;
BOOL bExists;
begin
bExists = FALSE;
// Create ADO Connection Object to connect to the SQL server
szADOConnObjID = "ADODB.Connection";
set pADOConnObj = CreateObject(szADOConnObjID);