473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

shrink SQL database in VB.net

91
Hi

I need to backup sql database, below is my VB code to do backup, shrink and delete. But I have problem to shrink the database. Need your help to find what's wrong for the shrink potion. the error happened when execute CmdShrink.ExecuteNonQuery(). thanks

Expand|Select|Wrap|Line Numbers
  1. Dim Cnt As Integer
  2.         Dim CmdBackup As New SqlClient.SqlCommand("backup database EM_AWARE to disk= 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\EMAwareBackup.bak'", Me.myConnection)
  3.         CmdBackup.CommandTimeout = 3600
  4.         Dim CmdShrink As New SqlClient.SqlCommand("shrink database EM_AWARE" me.myConnection)
  5.         Dim CmdDelete = New SqlCommand("DELETE from EM_AWARE_20kV", myConnection)
  6.  
  7.         Try
  8.             Me.myConnection.Open()
  9.             CmdBackup.ExecuteNonQuery()
  10.         Catch ex As Exception
  11.             MsgBox("error in frmESDMonitor, backup database:" & ex.Message.ToString)
  12.         End Try
  13.         Me.myConnection.Close()
  14.  
  15.         Cursor.Current = Cursors.WaitCursor
  16.         Try
  17.             Me.myConnection.Open()
  18.             CmdShrink.ExecuteNonQuery()
  19.         Catch ex As Exception
  20.             MsgBox("error in frmESDMonitor, shrink database:" & ex.Message.ToString)
  21.         End Try
  22.         Me.myConnection.Close()
  23.  
  24.         MsgBox("Going to delete all data")
  25.         Try
  26.             Me.myConnection.Open()
  27.             myCommand.CommandTimeout = 1800
  28.             Cnt = myCommand.ExecuteNonQuery()
  29.         Catch ex As Exception
  30.             MsgBox("error in frmESDMonitor, delete database:" & ex.Message.ToString)
  31.         End Try
  32.         Me.myConnection.Close()
  33.  
  34.  
  35.  
Aug 2 '10 #1
2 10270
PsychoCoder
465 Expert Mod 256MB
Take a look at using DBCC SHRINKFILE, like this

Expand|Select|Wrap|Line Numbers
  1. Dim CmdShrink As New SqlClient.SqlCommand("DBCC SHRINKFILE(EM_AWARE)" me.myConnection)
  2.  
Where EM_AWARE is the name of the database you want to shrink. You'll also want to shrink the log file (most are named DBNAME_Log) where DBNAME is the name of your database, in your case it would probably be EM_AWARE_Log so after you shrink the db itself then shrink the log file

Expand|Select|Wrap|Line Numbers
  1. Dim CmdShrink As New SqlClient.SqlCommand("DBCC SHRINKFILE(EM_AWARE_Log)" me.myConnection)
  2.  
Aug 2 '10 #2
qfchen
91
thank you very much, it works. the actual commands are:

Expand|Select|Wrap|Line Numbers
  1. Dim CmdDeleteRT As New SqlClient.SqlCommand("DELETE from EM_AWARE_RT", myConnection)
  2.         CmdDeleteRT.CommandTimeout = 1800
  3.  
  4.         Dim CmdShrink As New SqlCommand("DBCC SHRINKDATABASE(EM_AWARE)", Me.myConnection)
  5.         CmdShrink.CommandTimeout = 1800
  6.  
Aug 5 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: dchow | last post by:
Our database size is currently 4G and is incrementing at a rate of 45M/day. What is the max size of a SQL database? And what is the size beyond which the server performance will start to go down?
1
by: R Camarda | last post by:
Help, I have a database that has a data file of 2GB and a log file of 31GB. In enterprise manager, when I choose shrink it says there is 30GB of unused space. When I shrink the database, it does...
5
by: BashiraInTrouble | last post by:
Hi Friends, I have tried almost everything but I cant seem to shrink the transaction log. Executing DBCC SQLPERF(LOGSPACE) gives me this info: Database Log Size (MB) Log Space Used (%) ...
4
by: Tommy.Vincent | last post by:
hi all, This will be a easy question for all out here. I have a database of 28GB. having 3 Data Files 22 GB, 3.58 Gb and 2.70 GB respectively. and a Transaction Log file of 156 mb. When i...
13
by: Matik | last post by:
Hello everybody, First: SQL Server 2000 sp3a, HP cluster server, MS 2003 server, database recovery model simple Torn page detection: When I have this option turned on, processes conected with...
12
by: hallpa1 | last post by:
Hi all, I posted messages before about trying to purge many records (about 35%) of a 200Gig database. The responses gave me a lot to think about, especially regarding the indexes. But due to the...
30
by: Neil | last post by:
Yikes! My database, which had been consistently 1 gig for a long time, went from being 1 gig to 3 gigs overnight! Looking at the nightly backups, the database increased on average about 5-15 MB per...
5
by: Roger | last post by:
backup log testdb with truncate_only DBCC SHRINKFILE (testdb_log, 100) WITH NO_INFOMSGS backup database testdb to disk = '\\DC01\Backups\DB01\testdb.bak' with init and does the shrinkfile...
8
by: lbseong | last post by:
Good day, I felt this forum is good because the reply is fast and relevent. so, this is my second question... I am working on SQL2000, one of my db is use up around 250gb, and the hard disk...
6
by: Major Drake | last post by:
Windows 2003 64 bit sp2 + SQL Server std 32 bit sp2 compability mode 90, recovery model simple. I have about 40 Gb database where is about 98% free space (I deleted data from tables). dbcc...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.