Hi...
I am working in ADO.NET and use Truncate Command in particular table. How can we use this command?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Jan 18, 2012, 3:58 AM
Removes all rows from a table without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources.
The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log.
SqlConnection con = new SqlConnection("database Connection String");
con.Open();
SqlCommand cmd = new SqlCommand("TRUNCATE TABLE table_name",con);
cmd.ExecuteNonQuery();
con.Close();
hope this help.
Sam HobbsPosted Jan 18, 2012, 2:40 AM
Prabhu RajaPosted Jan 18, 2012, 2:38 AM
TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column. If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.
TRUNCATE TABLE may not be used on tables participating in an indexed view. You can Use Truncate Command in Ado.Net as,
using (SqlConnection con = new SqlConnection("connection string here "))
{
SqlCommand com = new SqlCommand("Truncate Table TableName ", con);
con.Open();
bool Deleted = com.ExecuteNonQuery() > 0;
}
If this Post helps you, then mark as "Correct Answer". thank You
Satyapriya NayakPosted Jan 18, 2012, 1:22 AM
Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.
TRUNCATE is faster and uses fewer system and transaction log resources than DELETE.
TRUNCATE removes the data by deallocating the data pages used to store the table's data, and only the page deallocations are recorded in the transaction log.
TRUNCATE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column.You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint.Because TRUNCATE TABLE is not logged, it cannot activate a trigger.
TRUNCATE can not be Rolled back.
TRUNCATE is DDL Command.
TRUNCATE Resets identity of the table.
Ex:-
TRUNCATE TABLE tablename
Thanks
Datta KharadPosted Jan 18, 2012, 12:36 AM
Truncate Command:-
Removes all rows from a table without logging the individual row deletes. TRUNCATE TABLE is functionally identical to DELETE statement with no WHERE clause: both remove all rows in the table. But TRUNCATE TABLE is faster and uses fewer system and transaction log resources than DELETE.
Syntax:-
TRUNCATE TABLE table_name;
Example:-
SqlCommand cmd;
SqlConnection con;
con=new SqlConnection("Connection String here");
con.Open();
string strSql = "TRUNCATE TABLE table_name";
cmd=new SqlCommand(strSql,con);
cmd.ExecuteNonQuery();