Click here to Skip to main content
15,920,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to delete multiple record with one sql statement,
but it is not working instead of deleting specific records it delete the whole table:
my code is:

string[] names = { "a.jpg","b.jpg","c.jpg"};

for (int j = 0; j<names.length;>
{

 OleDbCommand sqlcmd = new OleDbCommand("delete from table1 where name not in ('" + names[j] + "')", sqlconnection);
 sqlcmd.Parameters.AddWithValue("name", names[j]);

sqlcmd.executenonquery();


}

I am using C# and Access
Thanks for your help and reading the question.
Have nice day

[Edited]Code is wrapped in "pre" tags[/Edited]
Posted
Updated 24-Jan-11 22:46pm
v2

I think you need to use comma (,) in the string

C#
string names = "'a.jpg', 'b.jpg', 'c.jpg'";

OleDbCommand sqlcmd = new OleDbCommand("delete from table1 where name not in (" + names + ")", sqlconnection);

sqlcmd.executenonquery();
 
Share this answer
 
v3
Comments
bachasafyan 25-Jan-11 4:42am    
i used the comma in the string, it excute but the result is same, it delete the whole table. and it override the where clause condition :(
bachasafyan 25-Jan-11 4:55am    
@amit kumar,
thanks and your code did help the only thing i did was to add a for loop to loop through array values.
Wild-Programmer 25-Jan-11 4:59am    
Welcome mate, check this if you like http://www.fmsinc.com/MicrosoftAccess/query/snytax/delete-query.html
string[] names = { " 'a.jpg', 'b.jpg','c.jpg','j.jpg' "};


for (int j = 0; j<names.length;>
{

OleDbCommand sqlcmd = new OleDbCommand("delete from table1 where column_name not in (" + names[j] + ")", sqlconnection);

sqlcmd.executenonquery();

}

// this code will delete multiple records excluding the parameter values, in this case (names array).
you can exclude or include as many values as many you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900