Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I would like to Compress the database in SQL Server 2012 using .net code.

First I tried with the following SQL queries:

SQL
DBCC SHRINKDATABASE(DatabaseName);

GO
ALTER DATABASE DatabaseName SET RECOVERY SIMPLE
GO
DBCC SHRINKFILE (DatabaseName_log, 5)
GO
ALTER DATABASE DatabaseName SET RECOVERY FULL
GO


When I tries to execute the above queries in SQL server directly, it works properly; by executing in Stored procedure as well.

But when I tried to call the stored procedure from .net code, it gives e the exception of {"Cannot perform a shrinkdatabase operation inside a user transaction. Terminate the transaction and reissue the statement."}.

Kindly guide me if there is any problem with the queries, or if there is another solution to Compress the database from .net code.

Thanks.
Posted
Updated 28-Sep-14 22:21pm
v2
Comments
Sinisa Hajnal 29-Sep-14 4:49am    
Post your code if that is the problem. We cannot solve anything without it. I'd say you have BeginTransaction somewhere in the code :)
Mukul Lad 29-Sep-14 5:54am    
I have done it using Entity framework. Here, it is the code.

using (EntityName db = new EntityName())
{
db.spCompressDatabase("DB Name");
}
Sinisa Hajnal 29-Sep-14 6:29am    
Well then, you have to delve into EntityName and find out where it opens the transaction :)
Mukul Lad 29-Sep-14 8:15am    
Sorry, but I am not getting what you want to inform.
Kornfeld Eliyahu Peter 29-Sep-14 6:37am    
Use ExecuteNonQuery - http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx

1 solution

During the analysis, I found that the database compress feature is only available in Enterprise version of SQL server. Is it so?
Is it not available in express edition.

If anybody know, kindly confirm.

Thanks.
 
Share this answer
 
v2
Comments
Kornfeld Eliyahu Peter 29-Sep-14 8:20am    
No.
But it need proper admin permissions to run...
Mukul Lad 29-Sep-14 9:21am    
Can you please guide how to do so?
I am not able find it.

And, Please check the following URL:
http://msdn.microsoft.com/en-IN/library/cc645993.aspx

I found in this URL about express and enterprise edition.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900