英文:
ADO.net SqlParameter using a + sign between words
问题
I am using ADO.net and using SqlParameter
passing into a stored procedure a group of words that sometimes may have a +
between then. Up until the plus sign the search is working fine but once I add it I am getting no results. Do I need to change anything in the c# code I am run the sp fine with the plus sign.
c# code
SqlParameter[] parms = new SqlParameter[] {
new SqlParameter("@Title", SqlDbType.VarChar)
};
parms[0].Value = Title;
SQL SP
create PROCEDURE [usp_Select_Title] -
-- Add the parameters for the stored procedure here
@Title nvarchar(512) = null
WHERE (dcc.Title like '%' + @Title + '%')
英文:
I am using ADO.net and using SqlParameter
passing into a stored procedure a group of words that sometimes may have a +
between then. Up until the plus sign the search is working fine but once I add it I am getting no results. Do I need to change anything in the c# code I am run the sp fine with the plus sign.
c# code
SqlParameter[] parms = new SqlParameter[] {
new SqlParameter("@Title", SqlDbType.VarChar)
};
parms[0].Value =Title;
SQL SP
create PROCEDURE [usp_Select_Title] -
-- Add the parameters for the stored procedure here
@Title nvarchar(512) = null
WHERE (dcc.Title like '%'+ @Title +'%')
答案1
得分: 2
根据评论中的讨论,该值是通过URL中的查询字符串传递的。在这种情况下,+号在查询字符串中具有语义意义,用于表示空格。
因此,解决此问题的方法是在URL中将+号编码为%2B
,然后在C#中解码回+号。
参考:
- https://stackoverflow.com/questions/30262783/allow-plus-sign-in-url
- https://stackoverflow.com/questions/6855624/plus-sign-in-query-string
英文:
As per the discussion in comment, the value was passed to via query string in URL. In this the case the + sign has a semantic meaning in the query string which is used to represent a space.
So, the solution for this is to encode the + sign with %2B
in URL and decode back to + in C#.
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论