how can I delete SQL DB by VB?

Hello everybody,

I’m new to Visual Basic 2005. just learning now. I want to delete MS SQL Database while its running. see my codes.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim SQLConn As New SqlConnection("Integrated Security=SSPI;Initial Catalog=JJpack;Data Source=(local);User Id=sa")

SQLConn.Open()

Dim SQLda As New SqlDataAdapter("select * from countrymaster", SQLConn)

Dim SQLDS As New DataSet

SQLda.Fill(SQLDS)

ComboBox1.DataSource = SQLDS.Tables(0)

ComboBox1.DisplayMember = "Name"

ComboBox1.ValueMember = "CountryID"

SQLda = Nothing

SQLDS = Nothing

SQLConn.Close()

End Sub

This code working fine. Its loading all datas to combo box. But problem is at this time I need to delete database (“JJPack”), its also located in local server. While I try to remove the database, its give an error. message. Error is “Cannot drop the database 'jjpack' because it is currently in use.”.

See my delete code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If MessageBox.Show("Are you sure want to Delete Database ", "Delete...", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification, False) = Windows.Forms.DialogResult.Yes Then

Dim SQLCon5 As New SqlConnection("Integrated Security=SSPI;Initial Catalog=master;Data Source=(local);User Id=sa")

Dim SQLCom5 As New SqlCommand("drop database JJpack", SQLCon5)

Try

SQLCon5.Open()

SQLCom5.ExecuteReader()

MessageBox.Show("Database Deleted!")

Catch err As Exception

MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

Finally

SQLCom5 = Nothing

SQLCon5.Close()

End Try

End If

End Sub

HOW CAN I DELETE DATABASE BY VB anyone help me pls




Answer this question

how can I delete SQL DB by VB?

  • adorer

    try setting your select command to this:

    use [master] drop database JJpack

    does this help



  • kmazur

    I've run into this same problem, both when attempting to DROP a database, or issue a RESTORE DATABASE command. I've never found a VB solution, but if you're interested I have found a way using the command prompt and the SQLCMD utility supplied with VS.
  • arkiboys

    Thanks for reply

    but its give same error... :(



  • KeeperMustDie

    There was a similar question, take a look at this:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=585193&SiteID=1

     

    From that, try to:

    1) drop the initial catalog from the connection string and see if the command I gave works for the Select command

    2) set the Pooling=false in the connection string and then execute the drop database command and see if that works



  • how can I delete SQL DB by VB?