SQL Server 在 nvarchar 中搜索 &文本 [英] SQL Server search in nvarchar & ntext
问题描述
我在 VS2010、C# ASP.NET Web 应用程序中使用 SQL Server 2008 作为我的数据库引擎.我的项目是波斯语(波斯语),所以我使用 nvarchar
和 ntext
作为我的数据类型.
I'm using SQL Server 2008 as my database engine in a VS2010, C# ASP.NET web app. My project is Farsi (Persian) so I've used nvarchar
and ntext
as my data types.
我使用以下查询从我的数据库中查找行,但没有返回任何内容,而我有一行包含指定的关键字.当然,我的关键字是波斯语(unicode).
I use following query to find rows from my database but nothing is returned, while I have a row with the specified keyword. Of course my keyword is in Persian (unicode).
这里出了什么问题?是因为使用波斯语吗?如何在包含 unicode 字符的 nvarchar
和 ntext
列中进行搜索?
What is going wrong here? Is it because of using Farsi language? How can I search in nvarchar
and ntext
columns containing unicode characters?
myCommand = new SqlCommand("select * from tblArticle where name LIKE '%" + txtSearch.Text + "%'", SQLConnection);
推荐答案
您应该使用 N'
前缀来表示您正在搜索 Unicode 字符串:
You should use the N'
prefix to indicate that you're searching for a Unicode string:
SELECT * FROM dbo.tblArticle WHERE name LIKE N'%......%'
否则,您将搜索字符串转换回非 Unicode,然后搜索....
Otherwise, you're converting your search string back to non-Unicode and then searching....
这篇关于SQL Server 在 nvarchar 中搜索 &文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!