1: If the parameter is of the int type, declare @ axmlset @ arowid1idrowrowid5idrowrowid4idrowrowid3idrowrowid2idrowselect * fromproductwhereidin (selectd. x. value (. id [1], int) from@a.nodes (*) asd (x) 2: If the parameter is var
1: If the parameter is of the int type: declare @ a xml set @ a = 'rowid1/id/row rowid5/id/row rowid4/id/row rowid3/id/row rowid2/id/row' select * from product where id in (select d. x. value ('. /id [1] ', 'int') from @. nodes ('/*') as d (x) 2: If the parameter is var
1: IfParametersYes intType:
Declare @ a xml
Set @ a ='
1
5
4
3
2 '
Select * from product where id in (
Select d. x. value ('./id [1]', 'int') from @ a. nodes ('/*') as d (x ))
2: IfParametersYes varcharType:
Declare @ a xml
Set @ a ='
A
B5
C4
D3
E2 '
Select * from product where pname in (
Select d. x. value ('./name [1]', 'varchar (100) ') from @ a. nodes ('/* ') as d (x ))
C # simple method written in the first example
Public DataSet GetData (List
IdList) {System. Text. StringBuilder idXML = new System. Text. StringBuilder (); // converts IdList to idxml (the xml to be used later)
Parameters) Foreach (var item in idList) {idXML. AppendFormat ("
{0}
", Item);} System. text. stringBuilder strSql = new System. text. stringBuilder (); strSql. append ("select * from product where id in ("); // Parse xml
Parameters@ A extracts the ID. Here we can think of @ a as a special table strSql. append ("select d. x. value ('. /id [1] ', 'int') from @. nodes ('/*') as d (x) "); strSql. append (")"); SqlConnection con = new SqlConnection ("database connection string"); SqlCommand cmd = new SqlCommand (strSql. toString (), con );//
ParametersAssign SqlParameter [] para = new SqlParameter [] {new SqlParameter ("@ a", SqlDbType. xml) {Value = idXML. toString () }}; cmd. parameters = para ;//
QuerySqlDataAdapter sda = new SqlDataAdapter (cmd); DataSet ds = new DataSet (); sda. Fill (ds); return ds ;}
If you think it is a good article, please do not mean your suggestion. It is not easy to write an article.