[C++ CLI] How to output unicode with string (? marks)

Hello, i need one small help with charset selection in mysql.data when i make app in C++/CLI. I have one form where i can select "banned player" from dropdown and then program write text into textboxes from mysql where i can edit and when i press bottom button, then program upload changes to mysql, everything works, but charset is broken. Can someone help me how to solve this? I try to add in querystring on front "SET NAMES \'utf8\'; SET CHARACTER SET utf8; UPDATE...." but not work. Here is picture with problem http://prntscr.com/b1rei8

Thanks for help :)
Last edited on
Have a look here, maybe it will shed some light on your problem. The post is about C# but should also work with C++.
http://stackoverflow.com/questions/942277/mysql-c-sharp-text-encoding-problems
Thanks for reply. I try it but not work, but i see problem is in program, because program sending broken charset into mysql, i make wrong syntax to see what i sending, and i see this, look: http://prntscr.com/b1rxks
Problem is, in String^ something is correctly loaded unicode char but in string something is broken unicode charset.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
		Ziskanie hodnot z textboxov
		*/

		String^ ipAdresa = textBox_ipAdresa->Text;
		String^ steamId = textBox_steamId->Text;
		String^ menoHraca = textBox_menoHraca->Text;
		String^ dovodBanu = textBox_DovodBanu->Text; //Correct unicode charset
		String^ banId = label_idBanu->Text;



		/*
		Koniec ziskavania hodnot z textboxov
		*/
		string buffer = "SET NAMES UTF8; UPDATE sourcebans.sbns_bans SET ip=\"" + marshal_as<string>(ipAdresa) + "\", authid=\"" + marshal_as<string>(steamId) + "\", name=N\"" + marshal_as<string>(menoHraca) + "\", reason=N\"" + marshal_as<string>(dovodBanu) + "\" WHERE bid=" + marshal_as<string>(banId) + ";";
		String^ query = marshal_as<String^>(buffer);
		String^ constring = L"datasource=XXXXXXX; username=XXXXXXXX;password=XXXXXXXXXX;Charset=utf8";
		MySqlConnection^ connection = gcnew MySqlConnection(constring);
		MySqlCommand^ command = gcnew MySqlCommand(query, connection);

		MySqlDataReader^ myReader;
		try
		{
			connection->Open();
			myReader = command->ExecuteReader();
			label_banBolZmeneny->Text = "Ban bol upraveny a zapisany do banlistu!";
			button1->Enabled = false;
		}
		catch (Exception^ ex)
		{
			MessageBox::Show(ex->Message);
		}
	}


with marshal_as<string>(textBox_dovodBanu->Text) <- here is point where correct string get destroyed to ? marks.
Last edited on
Problem solved easily.
Im doing 2 step conversion at code on top, string buffer for mysql syntax and then conver string to String^ (and this is a problem where chars get broken to ?). easy fix is, do String^ buffer with clear text like "UPDATE ...." + ipAdresa, and now you get correct unicode :)
Well done.
BTW. It's easier to build your query string with String::Format. Also you don't need the std::string since the MySql classes are .NET and expect a String^.
Yea, now i finally know it how to make syntaxes correctly. Thanks :)
Last edited on
Topic archived. No new replies allowed.