【问题标题】:How to pass comma separated column values from sql sever in a variable in c#如何在c#中的变量中从sql server传递逗号分隔的列值
【发布时间】:2020-02-13 10:10:28
【问题描述】:

在我的表格中,我已经从多个下拉列表中插入了值,现在我想在我的 c# 代码的 where 子句中传递这些值,请帮助

SqlCommand command = new SqlCommand("select distinct comp_type from master_office_complaint 
where comp_id='" + reader["marked_officer"]+ "'", conn);


这里marked_officer 列包含多个值。

【问题讨论】:

  • 您需要一个WHERE comp_id IN (...) 子句。现在,我们的目标是保护该查询免受 SQL 注入。
  • 这能回答你的问题吗? Pass Array Parameter in SqlCommand
  • 我尝试过使用 in 子句,但我得到零响应
  • 使用参数,为了避免 sql 注入,您的查询应该是: select distinct comp_type from master_office_complaint where comp_id in (select value from string_split(@marked_officer, ','))
  • @MarcGuillot 我收到此错误 -System.Data.SqlClient.SqlException: '必须声明标量变量“@marked_officer”。'

标签: javascript c# .net sql-server


【解决方案1】:

请在 SQL Server 上找到以下答案。

SELECT DISTINCT comp_type FROM master_office_complaint
WHERE comp_id IN ('ID1','ID2','ID3')

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2012-04-21
    • 2016-09-16
    相关资源
    最近更新 更多