0
DataView view = new DataView(dt);
DataTable distinctValues = view.ToTable(true, "ProductID");
DataRow[] myRows = distinctValues.Select();

i use this code for selecting certain column inside a DataTable. Now how can i use myRows[] values to delete the values in my database? For example:

cmd.CommandText = "Delete from tblindividualproduct where ProductID = myRows[]";
cmd.ExecuteNonQuery();

assuming that myRows[] has multiple values.

Mix Austria
  • 925
  • 2
  • 15
  • 35
  • it will depend on what your backend is, assuming you don't want to do a loop or use a DataAdapter – Conrad Frix Mar 17 '14 at 18:59
  • Do you mean something like http://stackoverflow.com/questions/337704/parameterizing-an-sql-in-clause? – MikeSmithDev Mar 17 '14 at 19:00
  • @ConradFrix: maybe using loop can be more easy to understand because the link MikeSmithDev gave me is too hard for me to understand how it works. im sorry, just a noob. – Mix Austria Mar 17 '14 at 19:09

1 Answers1

0

you can use IN operator, and create corresponding string:

var idList = String.Join(",", myRows.Select(u => u["ColumnName"].ToString()));
string sql = string.Format("delete from tblindividualproduct where ProductID IN({0})", idList);

you need to create something like this:

string sql = "delete from tblindividualproduct where ProductID IN (1, 2, 3)"
Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
  • `string sql = "delete from tblindividualproduct where ProductID IN (1, 2, 3)"` where should i put this? is it different in this `string sql = string.Format("delete from tblindividualproduct where ProductID IN({0})", idList);` ? – Mix Austria Mar 17 '14 at 20:01
  • no, this `string sql = "delete from tblindividualproduct where ProductID IN (1, 2, 3)"`, or something like that is what you need to create finally, totally you need to create an `delete` statement using `IN` operator using `string.Format()` – Amir Sherafatian Mar 18 '14 at 05:10