.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Post New Web Links

Drop Database

Posted By:      Posted Date: October 06, 2010    Points: 0   Category :Sql Server


I have the following:

if exists (select name from sys.databases where name = @DatabaseName)
  alter database @DatabaseName set single_user with rollback immediate
  drop database @DatabaseName

I get an error on the second go:

Incorrect sintax near go.

Basically I am checking if the database exists. If yes then close all connections to it and drop it.

What am I missing?

Am I closing the connections correctly?



View Complete Post

More Related Resource Links

how to in Insert database NULL if drop down list item is not selected ?

hi,   on my aspx I have the below DDL   <asp:DropDownList ID="ColorDDL" runat="server" DataSourceID="ColorObj"                                                 DataTextField="Size" DataValueField="BustId" Width="150px"                                                 AppendDataBoundItems="True">                                                    <asp:ListItem Value="-1">(Optional)</asp:ListItem>                             &n

Difference between truncate and drop table on database fragmentation


I have a group of developers that are creating stored procedures, in them they are dropping a table and re-creating the table and then continuing with the processing.  My question is if there is any difference between the effect of dropping a table and truncating the table on database fragmentatiom.




can not drop user from database


 I can not delete user from a database in sql2005 beta 3.
the message errror is :

TITLE: SQL Server Management Studio

Drop failed for User 'Amministratore'.  (Microsoft.SqlServer.Smo)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft SQL Server&ProdVer=9.00.0981.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Drop+User&LinkId=20476


An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)


The database principal owns a schema and cannot be dropped. (Microsoft SQL Server, Error: 15138)

in sql 2000 I can delete the user very easy, but in sql 2005 I don't understant How to do it.

Can not drop a database in SQL 2005


Hello all,

SQL 2005 Standard Edition X64 version 9.00.3042.00

I have an interesting problem here that I can not seem to figure out.  I am trying to drop a user database and it keeps failing.

First, I started off using a simple DROP statement:

DROP DATABASE database_name

I received an error saying it was in use.  No problem, I figured.  So I then tried to execute this:


No luck here either.  I received an error message saying a lock could not be placed on the database.  Try again later.  Hmm.  For kicks, I tried the detach database stored procedure:

exec sp_detach_db @dbname = 'database_name', @skipchecks = 'true', @keepfulltextindexfile = 'false'

No luck here either.  I got curious and tried to see what process was using the database:

SELECT * FROM sysprocesses WHERE dbid = DB_ID('database_name')

I got no results.  So apparently nothing is using it.

For added fun, I checked the database properties by going into SSMS and right clicking the database and selecting Properties.  This is where it gets interesting.  I received an error dialog that states:

Cannot show requested dialog.  Th

A correct way to drop MS SQL database


Hi all. I need to programmatically remove the MS SQL database which used in my application. Currently I'm using the following code for this:

SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=<PATH_TO_MDF_FILE>;Integrated Security=True;Pooling = False;User Instance=True;MultipleActiveResultSets=True");<br/>
SqlCommand alterCommand = new SqlCommand("ALTER DATABASE \"<PATH_TO_MDF_FILE>\" SET SINGLE_USER WITH ROLLBACK IMMEDIATE", conn);<br/>
SqlCommand dropCommand = new SqlCommand("DROP DATABASE \"<PATH_TO_MDF_FILE>\";", conn);<br/>
//Not sure that pools clearing actually needed<br/>
//"Cannot drop database "<PATH_TO_MDF_FILE>" because it is currently in use." exception is raised here<br/>

Cannot drop filegroup from Database in case when the file, asociated with it, is corrupted

I have the following problem:
Have a DB with 4 filegroups with patition function and schema and one table attached to the schema. After restarting the SQL Server one of the ndf files become corrupted. I don't have backup. I set the filegroup offline and set the DB to multiuser, but I cannot remove the filegroup or file attached to the filegroup or rename the filegroup. It is not recomended to transfer the data to the new table with new schema. Does anybady has an idea how to solve this issue?

can we attach database again after drop it

i want to know that we are able to attached database again after drop it.please clear my point....

Detach database with drop connections



What is the significance of dropping connections (by chosing drop connection option) while detaching the database? Can it create problems if I don't select this option?



Multiple database support with Entity Framework

One of the features introduced in Entity Framework is being database independent. Which mean each database provider can support Entity Framework by implementing its provider.

This feature allows you build applications independent from the underplaying database provider. In this post I'm going to show how to build an application that support multiple databases using same conceptual model made by Entity Framework.

Export Visio Database Table Names to Excel

If you use the Enterprise Architect edition of Microsoft Visio for data modeling regularly, then there is a good chance that at some point you've wanted to export just the table names into Excel. You might want to do this to map logical ERD entities to physical data model tables, track project status by entity, or track overlap between database versions.

MS SQL Server: Disconnect Users From Database - Kill User Session

If you ever wanted to restore your database from a SQL backup file (.bak), but there are still users connected to your database, the backup operation will fail causing the error: Exclusive access could not be obtained because the database is in use.

Binding the multiple drop down boxes in GridView using JQuery in ASP.NET

Here is the code for binding the multiple drop down boxes in GridView using JQuery in ASP.NET

Publishing SQL Server Database using Publishing Wizard : Tips & Tricks

We can use SQL Server Publishing Wizard to deploy our local Database to remote hosting server/production server.This feature is available in SQL Server 2008/VWD 2008.

Below is the complete step by step guide of this process.

Adding Rows in GridView without using a Database

This article describes on how to add rows in GridView without using a database. Basically the GridView will be populated with data based on the values entered in the TextBox on Button Click and retain the GridView data on post back.

Adding Multiple Columns and Rows in GridView without using a Database

This article describes on how to add multiple columns and rows in GridView without using a database. Basically the GridView will be populated with data based on the values entered in each TextBoxes on Button Click and retain the GridView data on post back.

ASP.NET Database Tutorial

This tutorial will show you how to display data using the .NET GridView Control, ASP.NET 2.0 and VB.NET

The GridView control is a powerful tool and is simple to implement.

First, you will need to import the System.Data.SqlClient namespace.

Connestion string create any database server

This links important to how to create connection string to any data base sever.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend