• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1993
  • Last Modified:

Using a table adapter how can I programatically delete all the records in a table and reset the identity

I know in query analyzer I could DELETE * FROM tbl    and TRUNCATE TABLE to resetthe identity...but how would I do this in c# using a table adapter?
0
Prysson
Asked:
Prysson
  • 2
1 Solution
 
Bob LearnedCommented:
You could execute the query by creating an SqlCommand and calling ExecuteNonQuery.

string connectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
 
using (SqlConnection connection = new SqlConnection(connectionString))
{
   connection.Open();
   string commandText = "TRUNCATE TABLE table1";
   using (SqlCommand command = new SqlCommand(commandText, connection))
   {
     command.ExecuteNonQuery();
   }
}

Open in new window

0
 
Bob LearnedCommented:
You wouldn't need a DELETE FROM query with the TRUNCATE TABLE command, since it removes all the rows faster, and resets the identity.

Bob
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now