Open SQL Management Studio
Right Click on the Database you want
Select Tasks -> Shrink -> Database
Click on Script (this is at the top of the Shink Database window, next to Help)
Select Script Action to New Query Window
Execute that script somewhere in your website like so
Dim ShrinkSql As String = "DBCC SHRINKDATABASE(N'YourDatabase')"
Dim ShrinkConn As New SqlConnection("connectionstring")
Dim ShrinkCmd As New SqlCommand(ShrinkSql, ShrinkConn)
ShrinkConn.Open()
ShrinkCmd.ExecuteNonQuery()
ShrinkConn.Close()
If this takes too long and your page is timing out you can change the timeout for the page using Server.ScriptTimeout
String ShrinkSql = "DBCC SHRINKDATABASE(N'YourDatabase')";
SqlConnection ShrinkConn = new SqlConnection("connectionstring");SqlCommand ShrinkCmd = new SqlCommand(ShrinkSql, ShrinkConn);
ShrinkConn.Open();
ShrinkCmd.ExecuteNonQuery();
ShrinkConn.Close();