SQL Server search in nvarchar & ntext

旧时模样 提交于 2019-12-01 03:11:47

问题


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.

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).

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);

回答1:


You should use the N' prefix to indicate that you're searching for a Unicode string:

SELECT * FROM dbo.tblArticle WHERE name LIKE N'%......%'

Otherwise, you're converting your search string back to non-Unicode and then searching....




回答2:


You must use COLLATE Latin1_General_BIN2 to get results. Works for Amharic for Ethiopians.

Select * From YourTableName where ColNameYouSearch LIKE N'%YourSerchCriteria%' COLLATE Latin1_General_BIN2"



回答3:


I am not familiar with C# ASP.NET, but using nvarchar or ntext shouldn't make any difference in query type. I am wandering you are only assigning your query command to a variable and not executing it.. should you do something like myCommand.execute() ?



来源:https://stackoverflow.com/questions/11258129/sql-server-search-in-nvarchar-ntext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!