AtarC5
AtarC5

Reputation: 413

Error with Ñ in ExecuteNonQuery with SQL Server

Hello and first of all thank you for reading. I have in a .txt file the query to execute by "ExecuteNonQuery" to verify if the tables exists, etc.

The problems come when I try to collect the "ñ" char from the .txt file and execute this in the ExecuteNonQuery (If I put the query in the SQL Server Management Studio it works). I must use this char because of it's include in the column's name 'año' which means 'year' in English.

PD: I tried putting '&ntilde' and it works, the ExecuteNonQuery didn't give me any error, but SqlServer doesn't recognize it.

Finally, I left a small example of the SQL Query on the .txt File:

CREATE TABLE[dbo].[autos]( 
id_auto int IDENTITY(1, 1) not null PRIMARY KEY, 
patente varchar(7) not null, 
marca varchar(12) not null, 
modelo varchar(12) not null, 
año int not null, 
comentarios_auto varchar(200),
fecha_registro date DEFAULT GetDate() not null) 

The code in C#:

 int counter = 0;
        string linea;
        string contenedor_texto;
        contenedor_texto = "";
        StreamReader file = new StreamReader("c:/Users/Natario/Desktop/test.txt");
        while ((linea = file.ReadLine()) != null)
        {
            contenedor_texto = contenedor_texto + linea;
            counter++;
        }
        string comando_consulta = contenedor_texto;
        comandoSQLbeta(comando_consulta,datosConexion);

PD2: comandoSQLbeta, only execute the SQLQuery which contains 'comando_consulta'. PD3: The parameter 'datosConexion' is a string which contains the connectionString

Upvotes: 1

Views: 128

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

Use square brackets to escape names with characters from alphabets other than Latin:

CREATE TABLE[dbo].[autos]( 
id_auto int IDENTITY(1, 1) not null PRIMARY KEY, 
patente varchar(7) not null, 
marca varchar(12) not null, 
modelo varchar(12) not null, 
[año] int not null, -- <<== Here
comentarios_auto varchar(200),
fecha_registro date DEFAULT GetDate() not null) 

Upvotes: 1

Related Questions