如果发现数据库的日志文件(ldf)膨胀的太厉害,可以采取以下两个步骤:
(1)将Recovery model改为Simple(默认为Full)
(2)运行DBCC shrinkdatabase命令压缩数据库
函数代码:
Public Shared Function ShrinkDatabase(ByVal dbConnection As SqlClient.SqlConnection, ByVal dbName As String) As Boolean
' Set the recovery mode to Simple and then shrink the database
Dim sSQL As String
sSQL = "ALTER DATABASE [" & dbName & "] SET RECOVERY SIMPLE"
Dim sqlCmd As New SqlClient.SqlCommand(sSQL, dbConnection)
sqlCmd.ExecuteNonQuery()
sSQL = "DBCC shrinkdatabase(N'" & dbName & "', TRUNCATEONLY )"
sqlCmd.CommandText = sSQL
sqlCmd.ExecuteNonQuery()
' Set the recovery mode to Simple and then shrink the database
Dim sSQL As String
sSQL = "ALTER DATABASE [" & dbName & "] SET RECOVERY SIMPLE"
Dim sqlCmd As New SqlClient.SqlCommand(sSQL, dbConnection)
sqlCmd.ExecuteNonQuery()
sSQL = "DBCC shrinkdatabase(N'" & dbName & "', TRUNCATEONLY )"
sqlCmd.CommandText = sSQL
sqlCmd.ExecuteNonQuery()
End Function