Zack_074
Zack_074

Reputation: 167

Delete rows from mysql server from a list of ID's C#

I'm trying to delete a series of rows in a MySQL table from a list of ID's in C#. There's an employeeID row in the table. Basically my question is what kind of syntax would I use?

Upvotes: 3

Views: 6535

Answers (3)

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171519

If you are using Dapper it would look something like this:

int[] ids = new int[]{1,2,3};
DbConnection cn = ... //open connection here
cn.Execute("delete from Employee where employeeID in @ids", new {ids});

Upvotes: 4

Orn Kristjansson
Orn Kristjansson

Reputation: 3485

delete from employee where employeeId in( 2,4,3,2,34 )

Upvotes: -1

mellamokb
mellamokb

Reputation: 56779

You would probably use an IN clause in your DELETE:

DELETE FROM `EmployeeTable` WHERE EmployeeID IN (2, 3, 4, 5, ...)

This could be implemented with the String.Join method to generate the list:

var query = "DELETE FROM `EmployeeTable` WHERE EmployeeID IN (" +
    String.Join(",", myArray) + ")";

Upvotes: 6

Related Questions