How to truncate table by code?

Topics: Core, General
Feb 13, 2012 at 10:35 AM

Hello!

I want to delete all data from some table and then insert a new data to this table.So first I want to truncate table.A lot of records will be in this table.How to do it?

Developer
Feb 13, 2012 at 2:31 PM

AFAIK there is no way to truncate the table with the built-in IRepository (just fetch all the records and then delete one-by-one). You can use HQL somewhow, that could be an option, or even raw SQL if it's not an issue for you that the module would be tied to a database vendor.

Feb 14, 2012 at 5:28 AM

How I can to use HQL or raw SQL?Can you give some examples or links where I can learn about this?Thanks!

Developer
Feb 14, 2012 at 6:37 PM

Sorry I haven't used either in Orchard, just have read they can be used through Orchard APIs. For HQL examples, search the solution for "hql" or "hqlquery".

Feb 15, 2012 at 5:00 AM

Thanks!

I solved this problem:

using (var session =_sessionLocator.For(typeof(SomeTableRecord))) { 
	var query = session.CreateSQLQuery(@"truncate table dbo.SomeTableRecord"); 
	query.ExecuteUpdate(); 
}