1

I am creating a web app in c#, and I have multiple id's.

This is how it looks:

string id = "17359,17355,17460,16834";

this is four different id's i stored in my string

now this is my sqlcommand

sqlcommand cmd=new sqlcommand("select * from trid where id!=@id",con);
sqlparameter[] param={
new sqlparameter("@id",id)
};

what i want is, i dont want data's with these id's 17359,17355,17460,16834

and i want to see the rest of the data

what i should do here??

id's are dynamic and can be less or more in numbers

Matt
  • 14,906
  • 27
  • 99
  • 149

3 Answers3

1

If your IDs are comma-separated, you can do it like this:

SqlCommand cmd = new SqlCommand("select * from trid where id NOT IN (@id)", con);

So you don't have to split the ID-string.

Wudge
  • 357
  • 1
  • 6
  • 14
0

Probably a dynamic way to do this but this will work if the ID's you don't want to see are static.

string id1 = "17359";
string id2 = "17355";
string id3 = "17460";
string id4 = "16834";

sqlcommand cmd=new sqlcommand("SELECT * FROM trid WHERE id NOT IN (@id1, @id2, @id3, @id4)",con);
sqlparameter[] param={
new sqlparameter("@id",id)
};
Matt
  • 14,906
  • 27
  • 99
  • 149
0

You have to create a function which will split your string and return the individual values which you can use in select statement like [select * from SELECT * FROM trid WHERE id NOT IN (select ID from dbo.splitcommaSep(@IDs))]

Kindly review following link for the function

http://www.aspsnippets.com/Articles/Split-and-convert-Comma-Separated-Delimited-String-to-Table-in-SQL-Server.aspx

Ikram Shah
  • 1,206
  • 1
  • 11
  • 12