在不带引号的SQL查询中插入字符串

时间:2018-11-08 09:52:38

标签: c#

我需要进行如下查询:

SELECT * FROM Table WHERE Row.DATA = value

我需要在DATA处经过SqlParameter。如果我做这样的事情:

string value = "DATA";
SqlCommand sql = new SqlCommand("SELECT * FROM Table WHERE Row.@Val = value");
sql.Parameters.Add("@Val", SqlDbType.VarChar).Value = value;

我收到以下无效的查询:

SELECT * FROM Table WHERE Row.'DATA' = value

1 个答案:

答案 0 :(得分:1)

string value = "DATA";
SqlCommand sql = new SqlCommand($"SELECT * FROM Table WHERE Row.{value} = value");