Could I shrink SQL 2005 database programmatically in ASP.NET 2.0?

Last post 05-08-2008 7:40 AM by kamahuja. 3 replies.

Sort Posts:

  • Could I shrink SQL 2005 database programmatically in ASP.NET 2.0?

    08-30-2007, 10:57 PM
    • Loading...
    • mycwcgr
    • Joined on 08-07-2005, 8:47 AM
    • Posts 489

    Could I shrink SQL 2005 database programmatically?, If so, would you please give me a sample code, thanks!

  • Re: Could I shrink SQL 2005 database programmatically in ASP.NET 2.0?

    08-31-2007, 1:20 AM
    Answer
    • Loading...
    • BradFMJ
    • Joined on 03-31-2006, 2:10 PM
    • New Jersey
    • Posts 129

    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();

  • Re: Could I shrink SQL 2005 database programmatically in ASP.NET 2.0?

    08-31-2007, 1:56 AM
    • Loading...
    • mycwcgr
    • Joined on 08-07-2005, 8:47 AM
    • Posts 489
  • Re: Could I shrink SQL 2005 database programmatically in ASP.NET 2.0?

    05-08-2008, 7:40 AM
    • Loading...
    • kamahuja
    • Joined on 05-08-2008, 11:36 AM
    • Posts 1

    Is there some similar code to shrink sql express database files. This code does not work with sql express. It gives a message "Could not find database 'MyDatabaseName'. Check sys.databases." Please Help.

Page 1 of 1 (4 items)