Sign in to follow this  
Followers 0
rahuroy

How to verify existence of Database and delete it ?

2 posts in this topic

I am writing script for installation of application. Before starting installing i have to verify existence of database and if it exists then i have to drop it.Below is snippet of code where i am connecting to server and running query.But after querying not able to drop database if it exists? Someone can give some suggestions

; $fAuthMode - Authorization mode (0 = Windows Logon, 1 = SQL) (default = 0)

OnAutoItExitRegister("_Exit")

Func _Exit()

MsgBox(0,"@ExitCode",@exitCode)

EndFunc

Global $Ignore, $Ini, $Installer, $Pid,$GUID,$squery,$result,$fAuthMode,$xnn,$ynn,$query

$Ini = @ScriptDir & "\Install.ini"

Global $username = IniRead($INI, "EMSettings", "IS_NET_API_LOGON_USERNAME", "")

Global $password = IniRead($INI, "EMSettings", "IS_NET_API_LOGON_PASSWORD", "")

Global $sqlserver = IniRead($INI, "EMSettings", "IS_SQLSERVER_SERVER", "")

Global $sqlusername = IniRead($INI, "EMSettings", "sqlloginname", "")

Global $sqlpwd = IniRead($INI, "EMSettings", "sqlpassword", "")

Global $emdbname = IniRead($INI, "EMSettings", "IS_SQLSERVER_DB", "")

Global $fAuthMode = IniRead($INI, "EMSettings", "AuthMode", "") ; $fAuthMode - Authorization mode (0 = Windows Logon, 1 = SQL) (default = 0)

Global $sQuery="select * from master.dbo.sysdatabases where name =' & $emdbname & '"

If $fAuthMode=0 Then

$xnn="Provider=SQLOLEDB;Data Source= " & $sqlserver & ";Trusted_Connection=Yes;"

$sqlCon = ObjCreate("ADODB.Connection")

$sqlCon.Open($xnn)

if @error Then

MsgBox(0, "ERROR", "Failed to connect to the database")

Exit

Else

MsgBox(0, "Success!", "Connection to database successful!")

EndIf

$query=_SQLQuery($sqlCon,$sQuery)

If $query=1 Then $sqlCon.Execute("DROP DATABASE " & $emdbname)

else

$ynn="Provider=sqloledb;Data Source=" & $sqlserver & ";Network Library=DBMSSOCN;User ID=" & $sqlusername &";Pwd=" & $sqlpwd & ";"

$sqlCon = ObjCreate("ADODB.Connection")

$sqlCon.Open($ynn)

if @error Then

MsgBox(0, "ERROR", "Failed to connect to the database")

Exit

Else

MsgBox(0, "Success!", "Connection to database successful!")

EndIf

$query=_SQLQuery($sqlCon,$sQuery)

MsgBox(0,"",$query)

If $query=1 Then $sqlCon.Execute("DROP DATABASE " & $emdbname)

EndIf

Func _SQLQuery($sqlCon, $sQuery)

If IsObj($sqlCon) Then Return $sqlCon.Execute($sQuery)

Return

EndFunc

Share this post


Link to post
Share on other sites



rahuroy,

Use conoslewrite to verify the value of $query from _SQLQuery.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0