SQL + Cyrillic support + Parameterized Query + Multiple Choice
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)
精彩评论