-1

I need help! I write software for database management for the course. I can not complete the transaction by inserting data. I am from Ukraine and use Ukrainian data on database, but the transaction is not completed bringing the error "Incorrect string value: '\ xD0 \ xB2' for column 'User_name' at row 1 "} System.Exception {MySql.Data .MySqlClient.MySqlException} " I read all the articles on the stack overflow but nothing helped me( i use SET NAMES 'utf8'(cp1251, utf8mb4, koi8r, win1251, cp866 and other) but nothing work(help, the problem may be encoded on the development environment? i use MySql.Data.MySqlClient or MySQL.dll connect code ->

connStr = "server= localhost;user=root;charset=utf8mb4;database=DB_Journal;port=3306;password=masterkey;"conn = new MySqlConnection(connStr);

and insert ->

conn.Open();
string sql = "insert into Users(User_id, User_name, User_surname, User_fname, Login, UType_id, Password,Secret,Answer) values (null, '" +
  textBox1.Text + "', '" + textBox2.Text + "', '"
                        + textBox4.Text + "', '" + textBox3.Text + "', '"
                        + usr + "', '" + pass + "', '"
                        + richTextBox1.Text + "', '"
                        + textBox7.Text + "')";
MySqlCommand cmd = new MySqlCommand(sql, conn);

cmd.ExecuteNonQuery();

(I'm sorry, I can write not correctly, but i study hard)

  • 2
    First thing to fix: stop building SQL like that. Use parameterized SQL instead. That may well fix the problem, but if nothing else it will close a gaping SQL Injection Attack vulnerability. – Jon Skeet May 08 '17 at 21:06
  • The first thing I would do is change to parameters instead of concatenating strings. Besides the SQL injection problems it prevents, it also solves problems like embedded quotes in string values and other character that cause syntax problems. – D Stanley May 08 '17 at 21:06
  • Check this question: http://stackoverflow.com/questions/10957238/incorrect-string-value-when-trying-to-insert-utf-8-into-mysql-via-jdbc – RedRight May 08 '17 at 21:26

1 Answers1

0

I don't know what you are trying to accomplish with SET NAMES, but it probably does not do what you think it does. (Besides, its documentation explicitly says that it won't work with utf8.)

It is kind of hard to tell without seeing your CREATE TABLE statement, but what is probably happening is that you have declared your User_name etc. columns as being of type CHAR or VARCHAR while in fact they should be of type NCHAR or NVARCHAR.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • yes you are right, I used the text varchar data may tell me what type of data to the most appropriate text fields? create table Users ( User_id int AUTO_INCREMENT, User_name VARCHAR (30) User_surname VARCHAR (30) User_fname VARCHAR (30) Login VARCHAR (30) not null, UType_id int not null, Password VARCHAR (30) Secret VARCHAR (150) Answer VARCHAR (30) primary key (User_id, Login) ) – Влад Кравченко May 09 '17 at 08:50
  • Since you were using `VARCHAR`, you should use `NVARCHAR`. (And, since you use this type, I don't think you need the `(30)` part.) – Mike Nakis May 09 '17 at 08:54
  • varchar (30) unless such a record does not say that this type of string length of 30 characters? when I edit their code and change the type to Nvarchar it did not identify, MySQL Server recognizes it as the type of data? – Влад Кравченко May 09 '17 at 09:00
  • I do not understand what you are saying. Anyway, if removing `(30)` does not work for you, then do not remove it. Use `NVARCHAR(30)`. The important part is the `N` part. – Mike Nakis May 09 '17 at 09:09
  • the database server does not understand the type NVARCHAR(30), it does not identify him! :( – Влад Кравченко May 09 '17 at 09:27
  • worked! but the server handed something strange (some characters – Влад Кравченко May 09 '17 at 09:32