Funkcja dodaje użytkownika do bazy danych serwera MS-SQL, zwraca true/false po wykonaniu.
function BOOL DB_CreateUser(sUserToCreate, sUserPwd, sServerName, sDBName, sDriver,
bTrustedConn, sUserSA, sUserSAPwd, bLog)
OBJECT objConn, objRST;
STRING sConnString;
STRING szADOConnObjID, sSQL;
BOOL bResult,bResult1;
begin
bResult = FALSE;
if (sDriver == "") then
sConnString = "Provider=sqloledb;";
else
sConnString = "Driver={" + sDriver + "};";
endif;
if (bTrustedConn) then
sConnString = sConnString + "server=" + sServerName + ";";
sConnString = sConnString + "database=" + sDBName + ";";
sConnString = sConnString + "Trusted_connection=Yes";
else
sConnString = sConnString + "server=" + sServerName + ";";
sConnString = sConnString + "uid=" + sUserSA + ";";
sConnString = sConnString + "pwd=" + sUserSAPwd + ";";
sConnString = sConnString + "database=" + sDBName;
endif;
try
szADOConnObjID = "ADODB.Connection";
set objConn = CreateObject(szADOConnObjID);
if (IsObject(objConn)) then
objConn.ConnectionString = sConnString;
objConn.Open;
try
sSQL = "IF NOT EXISTS (SELECT * FROM master.dbo.syslogins WHERE
loginname='"+sUserToCreate+"') \r\n";
sSQL = sSQL + "BEGIN \r\n";
sSQL = sSQL + "EXEC ('CREATE LOGIN "+sUserToCreate+" WITH PASSWORD=N''"+sUserPwd+"'',
DEFAULT_DATABASE="+sDBName+", CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF') \r\n";
sSQL = sSQL + "END ";
set objRST = objConn.Execute(sSQL);
bResult = TRUE; //Wykonano
catch
bResult = FALSE; //Nie wykonano
endcatch;
if !bResult then //Jezeli pierwszy sposob sie nie wykonal to drugi
try
sSQL = "IF NOT EXISTS (SELECT * FROM master.dbo.syslogins WHERE
loginname='"+sUserToCreate+"') \r\n";
sSQL = sSQL + "BEGIN \r\n";
sSQL = sSQL + "EXEC sp_addlogin @loginame = '"+sUserToCreate+"',
@passwd = '"+sUserPwd+"', @defdb = '"+sDBName+"' \r\n";
sSQL = sSQL + "END ";
set objRST = objConn.Execute(sSQL);
bResult1 = TRUE; //Wykonano
bResult = TRUE;
catch
bResult1 = FALSE; //Nie wykonano
endcatch;
endif;
if (!bResult) && (!bResult1) then
if (bLog) then WriteLog(1,"Błąd dodanie użytkownika \""+sUserToCreate+"\"
do serwera SQL"); endif;
endif;
objConn.Close;
endif;
set objConn = NOTHING;
set objRST = NOTHING;
catch
if (bLog) then WriteLog(1," - Nie można uzyskać połączenia z bazą
na użytkownika \""+sUserToCreate+"\""); endif;
endcatch;
return bResult;
end;
Brak komentarzy:
Prześlij komentarz