简体   繁体   中英

How to search “Polish” text from SQL Server using C#?

I have to search a Polish text from SQL database in my windows application. In the SQL Database, the field is in Polish language.

Eg: The DB fields are Id, Name and values are 1 , Ąćłń. I have a textbox for entering searching text. When I enter the textbox value "Ąćłń", now the result is empty. The select query is working fine with English characters. My question is how can I search a Polish text?

This should not be any different than searching English or any other language text....

  1. connect to your database
  2. fire off a parametrized search query, making sure to use nvarchar as your parameter type
  3. show the results!

So basically, something a bit like:

    public List<object> Search(string searchArgument)
    {
        string searchQry = "SELECT (list of columns) FROM dbo.YourTableName WHERE Name = @SearchArg;";

        // Define connection and command to use
        using (SqlConnection conn = new SqlConnection("....."))
        using (SqlCommand cmdSearch = new SqlCommand(searchQry, conn))
        {
            // add parameter to search command - make sure to define NVarChar and a valid length!
            cmdSearch.Parameters.Add("@SearchArg", SqlDbType.NVarChar, 50);

            // set parameter value
            cmdSearch.Parameters["@SearchArg"].Value = searchArgument;

            // open connection, execute query, handle results
            conn.Open();

            using (SqlDataReader rdr = cmdSearch.ExecuteReader())
            {
                // loop over rows returned
                while (rdr.Read())
                {
                    // do something with results - create your objects that you want to return...

                }

                // close reader 
                rdr.Close();
            }

            // close connection
            conn.Close();
        }
    } 

And using an OR Mapper like Entity Framework would make this even easier still.

  • Alter fulltext catalog CATALOGNAME REBUILD WITH ACCENT_SENSITIVITY=OFF;

  • Or specify an accent-sensitive collation in the query. For example:

     select * from users where name like 'HELEN%' collate SQL_Latin1_General_CP1_CI_AI 
    • If you don't have fulltext catalog (which you should) you can configure the database so that it is accent-sensitive; change the collation to SQL_Latin1_General_CP1_CI_AI.

Options 1 and 2 are simplest from a database administrator's perspective

Option 3 is simplest from a programmer's perspective

Other collation related tips

  • Version of SQL Server via Select @@Version
  • SQL Server collation via SELECT CONVERT (varchar, SERVERPROPERTY('collation'));
  • Table datatype definition of your Polish text (ie Nvarchar or varchar)

  • The ideal polish Collation is case insensitive SQL_Polish_Cp1250_CI_AS_KI_WI

  • Patch your SQL Server to the most recent level
  • Default SQL Server collation is SQL_Latin1_General_CP1_CI_AS
  • Review this Article

WITH ACCENT_SENSITIVITY = {ON|OFF}

Specifies if the catalog to be altered is accent-sensitive or accent-insensitive for full-text indexing and querying.

Notice here:

select * from member where name like 'müller' COLLATE German_PhoneBook_CI_AI 

it will find "müller", "mueller", and "Müller"

and

SELECT * FROM mytest WHERE SOUNDEX (col1 )=SOUNDEX ('Muller')

it will find "müller", "mueller", and "Müller"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM