我有我的 sql 查询,它使用变量中的“where”显示字段。我的变量是从函数传递的
字符串empCode,其值为“ !\\( ”
这是我的代码:
public List<int> GetSuccessAndFailedCountForTodayForAgent(string empCode)
{
var result = new List<int>();
string query = "SELECT (SELECT COUNT(*) FROM BusinessTransactions WHERE STATUS='Failed' AND ENTEREDDATE='" + DateTime.Now.Date.ToShortDateString() + "' AND AgentEmployeeCode='" + empCode + "') AS FAILED_COUNT, (SELECT COUNT(*) FROM BusinessTransactions WHERE STATUS='Completed' AND ENTEREDDATE='" + DateTime.Now.Date.ToShortDateString() + "' AND AgentEmployeeCode='" + empCode + "') AS SUCCESS_COUNT";
using (SqlConnection conn = new SqlConnection(asynchPaymentDBConnectionString))
{
conn.Open();
using (SqlCommand s = new SqlCommand(query, conn))
{
using (SqlDataReader reader = s.ExecuteReader())
{
try
{
if (reader.HasRows)
{
while (reader.Read())
{
result.Add(reader.GetInt32(0));
result.Add(reader.GetInt32(1));
}
}
}
catch (Exception ex)
{
//do something
}
}
}
}
return result;
}
在我的 C# 中,结果变为 0 - 0,当我尝试直接使用 sql server 时,它显示的结果为 2 - 0
字符串!\\(被视为!\(
如何使用我的字符串!\\(到我的 where 子句?
编辑:
我尝试使用参数添加:
s.Parameters.Add("@EmployeeCode", SqlDbType.NVarChar, 16);
s.Parameters["@EmployeeCode"].Value = empCode;
还是不行