Results 1 to 13 of 13

Thread: [RESOLVED] Drop Database

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    484

    Resolved [RESOLVED] Drop Database

    Dear all,

    I am trying to make a code in VB.Net to drop database programatically, but when I try to drop it, it said the database is currently in use.

    I use the following code:

    VB Code:
    1. Dim SQLString As String = _
    2.             "IF EXISTS (" & _
    3.             "SELECT * " & _
    4.             "FROM master..sysdatabases " & _
    5.             "WHERE Name = '" & newDatabaseName.Text & "')" & vbCrLf & _
    6.             "DROP DATABASE " & newDatabaseName.Text & ""
    7.  
    8.         Try
    9.             Dim SQLConnection As New SqlClient.SqlConnection
    10.             SQLConnection.ConnectionString = SQLConnectionString
    11.             Dim Command As New SqlClient.SqlCommand(SQLString, SQLConnection)
    12.    
    13.             SQLConnection.Open()
    14.             Command.ExecuteNonQuery()
    15.             SQLConnection.Close()
    16.  
    17.         Catch ex As Exception
    18.             MessageBox.Show(ex.Message)
    19.         End Try

    How can I overcome this?

    Thank you

    PlayKid

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Drop Database

    Transact-SQL Reference


    DROP DATABASE
    Removes one or more databases from Microsoft® SQL Server™. Removing a database deletes the database and the disk files used by the database.

    Syntax
    DROP DATABASE database_name [ ,...n ]

    Arguments
    database_name

    Specifies the name of the database to be removed. Execute sp_helpdb from the master database to see a list of databases.

    Remarks
    To use DROP DATABASE, the database context of the connection must be in the master database.

    DROP DATABASE removes damaged databases marked as suspect and removes the specified database. Before dropping a database used in replication, first remove replication. Any database published for transactional replication, or published or subscribed to merge replication cannot be dropped. For more information, see Administering and Monitoring Replication. If a database is damaged and replication cannot first be removed, in most cases you still can drop the database by marking it as an offline database.

    A dropped database can be re-created only by restoring a backup. You cannot drop a database currently in use (open for reading or writing by any user). When a database is dropped, the master database should be backed up.

    System databases (msdb, master, model, tempdb) cannot be dropped.

    Permissions
    DROP DATABASE permissions default to the database owner, members of the sysadmin and dbcreator fixed server roles, and are not transferable.

    Examples
    A. Drop a single database
    This example removes all references for the publishing database from the system tables.

    DROP DATABASE publishing

    B. Drop multiple databases
    This example removes all references for each of the listed databases from the system tables.

    DROP DATABASE pubs, newpubs


    See Also

    ALTER DATABASE

    CREATE DATABASE

    sp_dropdevice

    sp_helpdb

    sp_renamedb

    USE

    ©1988-2000 Microsoft Corporation. All Rights Reserved.
    "The dark side clouds everything. Impossible to see the future is."

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    484

    Re: Drop Database

    I have resolve it already....I have use master and alter table, set single_user with rollback immediate to drop the database....

  4. #4
    Junior Member
    Join Date
    Sep 2005
    Location
    Montreal, Qc
    Posts
    18

    Re: [RESOLVED] Drop Database

    PlayKid (or anyone reading this)

    I have the same problem you had, but I don't really understand your solution... Here is my code for my drop function:

    VB Code:
    1. 'This function deletes the database with it's table
    2.     ' - strDatabase:        Name of the database
    3.     Public Sub s_DropDatabase(ByVal strDatabase As String)
    4.         Try
    5.             s_ConnectDB("master")
    6.             'Settings for the command
    7.             m_objSqlCommand.Connection = m_objSqlConnection
    8.             m_objSqlCommand.CommandType = CommandType.Text
    9.             'Command to delete the database
    10.             m_objSqlCommand.CommandText = "DROP DATABASE " & strDatabase
    11.             m_strCurrentDB = m_objSqlConnection.Database 'Update the class variable
    12.             m_objSqlCommand.ExecuteNonQuery() 'Execute the DROP DATABASE command
    13.         Catch ex As Exception
    14.             'Error management. When an error occurs, the error number and its description is
    15.             'displayed in a message box
    16.             MessageBox.Show(ex.Message)
    17.         Finally
    18.             s_CloseConnection()
    19.         End Try
    20.     End Sub

    I'm always having this problem... I'm getting many headaches over this one... If you have a solution, I even tried to unplug my network cable just to be sure I'm the only one connected to my MSDE server and it still says the database is in use.

    Can you explain that thing about alter table? How does that help to delete a database? And what about that single_user thing?

    Tanx in advance,
    Phil

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    484

    Re: [RESOLVED] Drop Database

    VB Code:
    1. m_objSqlCommand.CommandText = "ALTER DATABASE " & strDatabase
    2.  & "" & vbcrlf _
    3. "SET SINGLE_USER WITH ROLLBACK IMMEDIATE" & vbcrlf & _
    4. "DROP DATABASE " & strDatabase & ""

    Try understand some T-SQL statement, it helps.......
    this command is telling the database to remove all the connection that the database currently is having, so then the database is not using currently, then drop it.

  6. #6
    Junior Member
    Join Date
    Sep 2005
    Location
    Montreal, Qc
    Posts
    18

    Re: [RESOLVED] Drop Database

    tanx PlayKid, I'm gonna take a look a that

    Phil

  7. #7
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    262

    Re: Drop Database

    Hi Asgorath, I would like to ask if you can show "playkid" code modified, please? Thank you so much.
    mannyso

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,947

    Re: [RESOLVED] Drop Database

    @Manny Somarriba This is a 19 year old thread. You can't expect help from the people who answered back then in 2005. Try starting your own new thread in the VB.Net general forum

  9. #9
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    262

    Re: [RESOLVED] Drop Database

    Thank you .Paul. I really appreciate your help and yours links
    mannyso

  10. #10
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    262

    Re: [RESOLVED] Drop Database

    Ok .Paul I will do, thank you.
    mannyso

  11. #11
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    262

    Re: [RESOLVED] Drop Database

    If that is impossible, you can create a batch file in DOS mode using the command DOS to delete completely. At least your database administrator set up a lock on it.
    mannyso

  12. #12
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    565

    Re: [RESOLVED] Drop Database

    Quote Originally Posted by .paul. View Post
    @Manny Somarriba This is a 19 year old thread. You can't expect help from the people who answered back then in 2005. Try starting your own new thread in the VB.Net general forum
    I believe Manny Somarriba is testing a bot with his account. He's been a member since 2001, barely posts, but replies to someone's comment from 2005. He then thanked you twice when there was no reason to, then continues like you never pointed out this post is old:

    Quote Originally Posted by Manny Somarriba View Post
    Thank you .Paul. I really appreciate your help and yours links
    Quote Originally Posted by Manny Somarriba View Post
    Ok .Paul I will do, thank you.
    Quote Originally Posted by Manny Somarriba View Post
    If that is impossible, you can create a batch file in DOS mode using the command DOS to delete completely. At least your database administrator set up a lock on it.
    Or is this just the way he is?
    Last edited by Peter Porter; Jan 18th, 2025 at 08:23 PM.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,947

    Re: [RESOLVED] Drop Database

    Quote Originally Posted by Peter Porter View Post
    I believe Manny Somarriba is testing a bot with his account. He's been a member since 2001, barely posts, but replies to someone's comment from 2005. He then thanked you twice when there was no reason to, then continues like you never pointed out this post is old:

    …Or is this just the way he is?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width