Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to search the data from column1 and column2, data on column1 is a name in japanese text in nvarchar were as column2 is in varchar format.i am trying to search from text box search in column2 fine but it doesnt work in column1 unless english text are added on it.

What I have tried:

if (textBox7.TextLength>0)
       {
           con.Open();
           DataTable ds = new DataTable();
         SqlDataAdapter adp = new SqlDataAdapter("SELECT *  FROM table1 WHERE [名前] LIKE '%" + textBox7.Text + "%' OR [モデル名] LIKE '%" + textBox7.Text + "%'", con);
           adp.Fill(ds);
            dataGridView1.DataSource = ds;
               con.Close();
              }

              else
              {
                  MessageBox.Show("モデル名を入力して検索してください");
              }
Posted
Updated 27-Feb-22 21:46pm
Comments
Richard Deeming 28-Feb-22 3:43am    

Fix the SQL Injection[^] vulnerability in your code, and you will almost certainly resolve your problem at the same time.
C#
SqlDataAdapter adp = new SqlDataAdapter("SELECT *  FROM table1 WHERE [名前] LIKE @search OR [モデル名] LIKE @search", con);
adp.SelectCommand.Parameters.Add(new SqlParameter("@search", SqlDbType.NVarChar) { Value = "%" + textBox7.Text + "%" });
 
Share this answer
 
Use the following code to file your NVarchar data column. The synonym for NVarchar is National Character Varying. While filtering data use 'N' at the start of data.
SqlDataAdapter adp = new SqlDataAdapter("SELECT *  FROM table1 WHERE [名前] LIKE N'%" + textBox7.Text + "%' OR [モデル名] LIKE '%" + textBox7.Text + "%'", con);
 
Share this answer
 
Comments
Richard Deeming 28-Feb-22 3:43am    
You have copied the SQL Injection[^] vulnerability from the question.
n.deny 28-Feb-22 19:39pm    
thankyou Richard. it's working well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900