-2

I am using this code to attempt to delete data from my table:

SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("delete * from login", con);
con.Open();
cmd.ExecuteNonQuery();

When I run this I receive this error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Incorrect syntax near '*'

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
M7off
  • 137
  • 1
  • 9

4 Answers4

3

The standard syntax for a DELETE statement is

 DELETE from <tablename> WHERE <condition>

No asterisk is needed before FROM. Just MS-Access likes that syntax.

Also keep in mind that if you don't add a WHERE condition, the whole table is deleted but this could be easier and faster with

 DROP TABLE <tablename>
Steve
  • 213,761
  • 22
  • 232
  • 286
1
SqlCommand cmd = new SqlCommand("delete from login", con);

You don't have to specify which column you are deleting, as you always delete entire rows. That's why you don't have to write *

Paolo Costa
  • 1,989
  • 1
  • 12
  • 15
0

Problem in your query.

Instead of writing:

Delete * from login

You should write:

Delete from login

Exp: in delete command we don't need to write * as in a select statement since you cannot delete only certain columns but the entire row. The where clause can still be applied, however, to select specific rows.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
0

Remove * and your query is as good as you are.