Pass Array Parameter in SqlCommand

前端 未结 12 2070
情深已故
情深已故 2020-11-22 08:27

I am trying to pass array parameter to SQL commnd in C# like below, but it does not work. Does anyone meet it before?

string sqlCommand = \"SELECT * from Ta         


        
12条回答
  •  庸人自扰
    2020-11-22 09:21

    If you can use a tool like "dapper", this can be simply:

    int[] ages = { 20, 21, 22 }; // could be any common list-like type
    var rows = connection.Query("SELECT * from TableA WHERE Age IN @ages",
              new { ages }).ToList();
    

    Dapper will handle unwrapping this to individual parameters for you.

提交回复
热议问题