Comma Delimited SQL string Need to separated

前端 未结 11 1966
[愿得一人]
[愿得一人] 2020-12-20 23:39

I have this string that i am getting from .net application A,B,C,D,E,F,

I wanted to write a sql select statement like

set @string = \'A,B,C,D,E,F\'

         


        
11条回答
  •  死守一世寂寞
    2020-12-21 00:25

    It think the easiest way to do it, will be, dynamic SQL generation:

    // assuming select is a SqlCommand
    string[] values = "A,B,C,D,E,F".Split(',');
    StringBuilder query = new StringBuilder();
    query.Append("select * from tbl_test where tbl_test.code in (");
    int i = 0;
    foreach (string value in values) {
        string paramName = "@p" + i++;
        query.Append(paramName);
        select.Parameters.AddWithValue(paramName, value);
    }
    query.Append(")");
    select.CommandText = query.ToString();
    
    // and then execute the select Command
    

提交回复
热议问题