简体   繁体   English

SQL更新命令始终返回0

[英]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. 该数据库是Access数据库。 I can insert and delete but not update the data. 我可以插入和删除但不能更新数据。 Its a Windows Forms C# App. 它是Windows Forms C#应用程序。

        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 注意:在CRUD方法中,我仅填写参数并执行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. 参数应与sql中的顺序相同。 In the sql itself, you need to use '?' 在sql本身中,您需要使用'?'

    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. 我一直想过使用一个将值与查询中的名称(例如id = @id)匹配的字典,这意味着将参数添加到OleDbCommand.Parameters列表时顺序无关紧要。 Indeed, I have never had this issue with .NET and Oracle or SQL Server databases. 确实,.NET和Oracle或SQL Server数据库从未遇到过此问题。 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). 但是,使用msaccess(或者可能是OleDb)时,我发现当id参数(请参阅下面的单行)作为OleDbCommand.Parameters列表中的第一项时,ExecuteNonQuery始终返回0。将行移至参数的末尾列表(在WHERE子句中使用了id),ExecuteNonQuery返回1(预期结果)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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