Code Snippet for detaching SQL database programmatically in vb.net

Following code is for detaching sql database programmatically

VB.Net Code :

Dim conn As SqlClient.SqlConnection

        Dim cmd As New SqlClient.SqlCommand()

        Dim ConnectionString As String = “Data Source=” & Server name & “;Initial Catalog=master;Integrated Security=True”  ‘ Connection string

        conn = New SqlClient.SqlConnection

        conn.ConnectionString = ConnectionString

        conn.Open()   ‘Open connection

        Dim sqlDetech As String = “sp_detach_db ‘”& database name &”‘, ‘true'”  ‘ Sql query for detaching database

        cmd.CommandText = sqlDetech

        cmd.Connection = conn

        cmd.ExecuteNonQuery()  ‘execute query