Friday, February 24, 2012

How to delete a table in Visual Studio 2005

I need to delete a database table from my database. I can not figure out how to do it though. I tried deleting individual columns but go an error message "Drop Failed for column 'TPListHistoryId ' (Microsoft.SqlServer.Smo)

If your DB is attached to your project in VS2005 then you can open it up in server explorer and rightclick onthe table and choose delete.

If you are using sqlserver express management studio then try DROP table <tablename>

|||

Do you need to do this from within an ASP page?

The syntax is:

DROP TABLE <tablename>

You can do this directly in the query analyzer if you open up a query window. Or you could do it from within an ASP.net page. In this case, the syntax is:

SqlConnection conn =new SqlConnection(string here>);
SqlCommand cmd =new SqlCommand("DROP TABLE <tablename>", conn);
cmd.ExecuteNonQuery();

Note: If this table is referenced by foreign constraints, then you cannot simply drop this table without disabling the constraints first. To do this, you must be absolutely positive that you know what you're doing, or you'll possibly end up breaking the integrity of your database.

Good luck!

|||

I am working strictly out of SQL Server 2005. Not visual studio. Sorry I made a mistake. So how can I delete the table? Do you know?

|||

Sorry. I meant to say that I am working directly out of SQL Server 2005. So I messed up in my post. I tried dropping the table in SQL Server 2005, but I get the following error message:

Msg 3726, Level 16, State 1, Line 1

Could not drop object 'Location' because it is referenced by a FOREIGN KEY constraint.

|||

You would need to drop the child tables first. What that message is telling you is that you can't drop the table because there is another table that has a foreign key constraint tide to it. This is put in place to help maintain data integrety.

|||

Check out this previous post for a discussion on how you do a cascading delete from the bottom up:

http://forums.asp.net/p/1144446/1854498.aspx

No comments:

Post a Comment