Export to database

3

I load 3 strings from a CMS.

String Data da Noticia
String Titulo da Noticia
String Texto da Noticia
Within a loop , I create the query to insert with these 3 strings.

I'm just having some problems.

1st) The string Text of the News, has characters like "-" and "'" to solve, I used replace . But would you have some way to insert, without having to remove those characters from the text of the news?

2º) Characters with accentuation of the text of the news such as "ç", "é", "á" are being saved in the bank as a "?"

I tried to use this:

pstmt = con.prepareStatement("SET NAMES 'utf8'");
pstmt.executeUpdate();
pstmt = con.prepareStatement("SET CHARACTER SET utf8");
pstmt.executeUpdate();

But it does not solve. In the bank I also set to utf8 and unicod.

Does anyone know how to solve these 2 problems?

    
asked by anonymous 08.07.2015 / 13:12

1 answer

1

MySQL is fully capable of saving special characters. I would advise you instead of using string concatenation to form a script .

Ex:

string script = "INSERT INTO TABELA (ID, NOME, IDADE) VALUES ("+ id +",'"+ nome +"'," + idade +")" 

Use variables to pass information.

Ex:

string script = "INSERT INTO TABELA (ID, NOME, IDADE) VALUES (@ID, @NOME, @IDADE)" 

var command = new SqlCommand(sql);

command.Parameters.AddWithValue("@ID", id);
command.Parameters.AddWithValue("@NOME", nome);
command.Parameters.AddWithValue("@IDADE", idade);
    
08.07.2015 / 14:09