I have this problem in C#, I have already run this query in SQL Server and works

SQL query

SELECT Mineria.dbo.Usuario.ID_Usuario, Mineria.dbo.Usuario.Sexo, Mineria.dbo.Usuario.Idioma, Mineria.dbo.Usuario.Edad, Mineria2.dbo.ARTISTA.Nombre_artistic
INTO Mineria.dbo.Objeto
FROM Mineria.dbo.Usuario
INNER JOIN Mineria2.dbo.ARTISTA ON Mineria2.dbo.ARTISTA.Id_Artista=Mineria.dbo.Usuario.ID_Usuario
AND Mineria.dbo.Usuario.ID_Usuario BETWEEN 35 AND 70
ORDER BY ID_Usuario ASC

I can't find the problem on the c# code

SqlCommand comando = new SqlCommand(string.Format("
Select '" + maskedTextBox1.Text + "' , '" + maskedTextBox2.Text + "' 
INTO Mineria.dbo.Objeto FROM Mineria.dbo.Usuario INNER JOIN Mineria2.dbo.ARTISTA ON Mineria2.dbo.ARTISTA.Id_Artista=Mineria.dbo.Usuario.ID_Usuario 
AND Mineria.dbo.Usuario.ID_Usuario BETWEEN '" + textBox1.Text + "' AND '" + textBox2.Text + "' ORDER BY ID_Usuario ASC"), cn);

1 Answers

1
Russell Fox On

It's probably because you're putting single-quotes around the field names (should be nothing since you're using fully-qualified names), and around the integers in the BETWEEN statement (should be nothing because they're numbers, not text):

SqlCommand comando = new SqlCommand(string.Format("
Select " + maskedTextBox1.Text + " , " + maskedTextBox2.Text + " 
INTO Mineria.dbo.Objeto FROM Mineria.dbo.Usuario INNER JOIN Mineria2.dbo.ARTISTA ON Mineria2.dbo.ARTISTA.Id_Artista=Mineria.dbo.Usuario.ID_Usuario 
AND Mineria.dbo.Usuario.ID_Usuario BETWEEN " + textBox1.Text + " AND " + textBox2.Text + " ORDER BY ID_Usuario ASC"), cn);