Jump to content

How to verify existence of Database and delete it ?


rahuroy
 Share

Recommended Posts

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...