Is there a way (similar to the below code) to delete all the rows in a specified table using c#?
SqlConnection con = new SqlConnection(conString);
con.Open();
string sql = @"DELETE*FROM compsTickers;";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
Right now i'm getting an error:
Incorrect syntax near '*'
Delete statement removes only the rows in the table and it preserves the table structure as same, and Drop command removes all the data in the table and the table structure.
An asterisk (" * ") can be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: The FROM clause, which indicates the table(s) to retrieve data from.
The SQL DELETE Query is used to delete the existing records from a table. You can use the WHERE clause with a DELETE query to delete the selected rows, otherwise all the records would be deleted.
There's nothing wrong with your C# code; that's an SQL syntax error.
Anyway, there's no need for the *
. You delete rows, not columns, from a table, so you don't specify columns to delete:
DELETE FROM compsTickers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With