开发者

SQL + Cyrillic support + Parameterized Query + Multiple Choice

开发者 https://www.devze.com 2023-04-02 11:54 出处:网络
I have this code so far: string userInput = Textbox1.text; string query = \"SELECT * FROM Books WHERE BookName = @Title\";

I have this code so far:

string userInput = Textbox1.text;
string query = "SELECT * FROM Books WHERE BookName = @Title";
SqlConnection con = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand(query, con); 
cmd.Parameters.AddWithValue("@Title", userInput);

and I need this query to support Cyrillic characters, and use N'' for Cyrillic support:

SELECT * FROM Books Where BookName = N'UserInput%'  

i need '%'开发者_如何学C too because i want to find all books that match the UserInput.


This is not tested since I don't use .NET but I would probably do it like this.

string UserInput = Textbox1.text;
string query = "SELECT * FROM Books WHERE BookName like @Title";
SqlConnection con = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand(query, con); 
cmd.Parameters.AddWithValue("@Title", UserInput+"%");

Change = to like in the query, add the % to the user input and make sure that the data type for BookName in table Books is of nvarchar.


You may use LIKE operator, for example

SELECT * FROM Books WHERE BookName LIKE @Title

And to assign parameter a value, you can simply append a "%" to the end of string

cmd.Parameters.AddWithValue("@Title", UserInput + "%");


You need Unicode support, not just Cyrillic support.

What is the type of Books.BookName? Is it NVARCHAR(x)?

System.String is already Unicode.

You may try to specify input type manually: cmd.Parameters.Add("@Title", SqlDbType.NVarChar, x)

0

精彩评论

暂无评论...
验证码 换一张
取 消