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

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
Seamie_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
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!
0
Seamie_Author Commented:
Hi Emoreau

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

Thanks

Seamie
0
Carl TawnSystems and Integration DeveloperCommented:
Change this line:

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

To:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Arthur_WoodCommented:
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
0
Seamie_Author Commented:
Yup, it worked thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.