System.Data.Sqlclient.Sqlexception: 'Incorrect syntax near 'messaging'. '

Asked

Viewed 120 times

-1

I tried everything, changed the codes, changed several things but the error prevails.

Code:

class Program
{



    public static void Main(string[] args)
    {
        List<string> listaProcesso = new List<string>();

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=CORPORATE02;Initial Catalog=ANDAMENTOS;Persist Security Info=True;User ID=textractor;Password=jfkhuk";
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from tbprocesso_teste where idcliente = 23915 and flag = 5", con);
        SqlDataReader reader = cmd.ExecuteReader();        
        while (reader.Read())
        {
            listaProcesso.Add(reader.GetString(21));

        }
        con.Close();                       
        
    List<string> listaProcessoConcertados = new List<string>();

        foreach (var elemento in listaProcesso)
        {
            var resultado = TratarErroJson(elemento);
            listaProcessoConcertados.Add(resultado);
       
        }
        

        foreach(var elemento in listaProcessoConcertados)
        {
            UpdateJson(elemento);
        }


    }

      public static string TratarErroJson(string jsonErrado)
      {

        var arquivo = jsonErrado;
        int posicao = arquivo.IndexOf("itensProcesso");     
        int posicao1 = arquivo.IndexOf("mensagemErro");
        if (posicao > -1)
        {                                                 
            string str = arquivo.Substring(0, posicao);
            string obj = str + "itensProcesso\":[]}";
            Console.WriteLine(obj);
            return obj;
        }else if (posicao1 > -1)
        {
            return jsonErrado;
        }
        else
        {
            return"";
        }

        
      }
      
       public static void  UpdateJson (object MyUpdate)      
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=CORPORATE02;Initial Catalog=ANDAMENTOS;Persist Security Info=True;User ID=textractor;Password=okmnji90";
        con.Open();
        string query = "update tbprocesso_teste set txdistribuicao=" + MyUpdate + " where idcliente = 23915 and flag = 5";
        SqlCommand cmd = new SqlCommand(query);
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        con.Close();

      
    }
    




}

}

1 answer

1

I believe the error is occurring in the Updatejson method, where the Myupdate parameter is added in the SQL statement without being delimited by quotes, although I think the best option would be to use parameter in the SQL statement:

public static void  UpdateJson (string MyUpdate)      
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=CORPORATE02;Initial Catalog=ANDAMENTOS;Persist Security Info=True;User ID=textractor;Password=okmnji90";
    con.Open();
    string query = "update tbprocesso_teste set txdistribuicao = @txdistribuicao where idcliente = 23915 and flag = 5";
    SqlCommand cmd = new SqlCommand(query);
    cmd.Parameters.AddWithValue("@txdistribuicao", MyUpdate);
    cmd.Connection = con;
    cmd.ExecuteNonQuery();
    con.Close();
}

Also note that I changed Myupdate to string because the code calling the method passes a string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.