We help IT Professionals succeed at work.

How do I use VB .Net to delete a table in SQL Server?

Seamie_
Seamie_ asked
on
Medium Priority
319 Views
Last Modified: 2010-04-23
Hi Experts,

The code attached below is crashing at the point marked <=***

Where in the previous code have I gone wrong?

Many thanks in advance,

Seamie

    Public Function DropDatabase(ByVal connString As String, ByVal sDBName As String) As Boolean
        Dim cn As New SqlConnection(connString)
        Try
            cn.Open()
            'Dim cmdDrop As New SqlCommand("DROP DATABASE " & sDBName, cn) [my note: I've alternated between this line and the next by remarking the other one out]
            Dim cmdDrop As New SqlCommand("DROP DATABASE " & sDBName)
            cmdDrop.ExecuteNonQuery()                                                              <=***
            WriteToFile("Setup", "Database Deleted")
        Catch ex As Exception
            MsgBox("Encountered an Error in deleting the database: " & sDBName & vbCrLf & "It is important that you contact the SQL Server Administrator and tell him/her that this database needs to be deleted!", MsgBoxStyle.Critical)
            WriteToFile("Setup", "Database not deleted (" & sDBName & ")" & vbCrLf & "Connection String= " & connString)
        Finally
            cn.Close()
        End Try
    End Function
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
Hi Seamie_,

What is the error message?

Is the user (used in your connection string) is allowed to drop database?

Your second declaration does not have any connection to run against.

Cheers!

Author

Commented:
Hi Emoreau

Error Message:=> ExecuteNonQuery: Connection Property has not been initialised

Thanks

Seamie
Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
Change this line:

    Dim cmdDrop As New SqlCommand("DROP DATABASE " & sDBName)

To:

    Dim cmdDrop As New SqlCommand("DROP DATABASE " & sDBName, cn)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
you must use the connection object along with the Command object, like this:

 cn.Open()
            'Dim cmdDrop As New SqlCommand("DROP DATABASE " & sDBName, cn) [my note: I've alternated between this line and the next by remarking the other one out]
            Dim cmdDrop As SqlCommand = New SqlCommand("DROP DATABASE " & sDBName, cn)
            cmdDrop.ExecuteNonQuery()                                                              <=***
            WriteToFile("Setup", "Database Deleted")
        Catch ex As Exception

when you say you have alternated between the two forms, what do you get with this syntax?

AW

Author

Commented:
Yup, it worked thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.