简体   繁体   中英

SQL update command return always 0

I wonder why this code doesn´t work; it always return zero (any affected row). The database is a Access database. I can insert and delete but not update the data. Its a Windows Forms C# App.

        string sql = "";
        try
        {
            string[] parametrosNomes = new string[3];
            parametrosNomes[0] = "@CatId";
            parametrosNomes[1] = "@CatNome";
            parametrosNomes[2] = "@CatDescricao";

            object[] parametrosValores = new object[3];
            parametrosValores[0] = oCateg.categoriaid;
            parametrosValores[1] = oCateg.categoriaNome;
            parametrosValores[2] = oCateg.categoriaDescricao;

            sql = "UPDATE Categorias SET categoriaNome = @CatNome, categoriaDescricao=@CatDescricao Where categoriaId=@CatId";
            int retorno = AcessoDB.CRUD(sql, parametrosNomes, parametrosValores);
            return retorno;
        }
        catch (Exception ex)
        {
            throw ex;
        }

Note: In CRUD method I only filling the parameters and executing the sqlcommand

int retorno = command.ExecuteNonQuery();
return retorno;

I verified all parameters, the name of fields and the command is executed without errors but always return zero and does not update the table in database.

I can´t see nothing wrong in my code but it doesn´t work.

Can anyone open my eyes?

Try this. Parameters should be in the same sequence as in the sql. In the sql itself, you need to use '?'

    string sql = "";
    try
    {
        string[] parametrosNomes = new string[3];
        parametrosNomes[0] = "@CatNome";
        parametrosNomes[1] = "@CatDescricao";
        parametrosNomes[2] = "@CatCatId";

        object[] parametrosValores = new object[3];
        parametrosValores[0] = oCateg.categoriaNome;
        parametrosValores[1] = oCateg.categoriaDescricao;
        parametrosValores[2] = oCateg.categoriaid;

        sql = "UPDATE Categorias SET categoriaNome = ?, categoriaDescricao = ? Where categoriaId = ?";
        int retorno = AcessoDB.CRUD(sql, parametrosNomes, parametrosValores);
        return retorno;
    }
    catch (Exception ex)
    {
        throw ex;
    }

I would like to thank everyone for the answers.

The problem was solved : the parameters are not being passed in the order they are being used.

I'm just correct the parameters order:

string[] parametrosNomes = new string[3];
parametrosNomes[0] = "@CatNome";
parametrosNomes[1] = "@CatDescricao";
parametrosNomes[2] = "@CatId";
object[] parametrosValores = new object[3];
parametrosValores[0] = oCateg.categoriaNome;
parametrosValores[1] = oCateg.categoriaDescricao;
parametrosValores[2] = oCateg.categoriaid;
sql = "UPDATE Categorias SET categoriaNome = @CatNome, categoriaDescricao=@CatDescricao Where categoriaId=@CatId";

Now the table is being updated correctly.

I had always thought using a dictionary that matched the values with the names (eg, id = @id) in the query meant the order didn't matter when adding parameters to the OleDbCommand.Parameters list. Indeed, I have never had this issue with .NET and Oracle or SQL Server databases. However, with msaccess (or maybe it's OleDb) I found when I had the id parameter (see single line below) as the first entry in the OleDbCommand.Parameters list, ExecuteNonQuery always returned 0. After moving the line to the end of the parameters list (id was used in the WHERE clause), ExecuteNonQuery returned 1 (the expected result).

cmd.Parameters.AddWithValue("@id", pi.Id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM