Add List to a mysql parameter

后端 未结 9 1319
暗喜
暗喜 2020-12-14 02:36

I have this question about the MySqlParameter from the .NET connector.

I have this query:

SELECT * FROM table WHERE id IN (@parameter)
9条回答
  •  悲&欢浪女
    2020-12-14 03:23

    you are going to have to iterate over your array and create the list yourself

    // no parameters
    var sb = new StringBuilder();
    for(int i=0;i0) {sb.Length-=1;}
    string sql = "SELECT * FROM table WHERE id IN (" + sb.ToString() + ")";
    

    UPDATE: Having thought more about this I'll go back to my original answer (below) which is to use parameters. Optimisations of built queries and whatever the database engine can muster are up to you.

    // no parameters
    var sb = new StringBuilder();
    for(int i=0;i0) {sb.Length-=1;}
    string sql = "SELECT * FROM table WHERE id IN (" + sb.ToString() + ")";
    

提交回复
热议问题