C# SQL更新命令始终返回0

C# SQL更新命令始终返回0,c#,sql,winforms,ms-access-2007,C#,Sql,Winforms,Ms Access 2007,我想知道为什么这个代码不起作用;它总是返回零(任何受影响的行)。该数据库是Access数据库。我可以插入和删除数据,但不能更新数据。这是一个Windows Forms C#应用程序 注意:在CRUD方法中,我只填充参数并执行sqlcommand int retorno = command.ExecuteNonQuery(); return retorno; 我验证了所有参数、字段名称和命令执行时没有错误,但始终返回零,并且不更新数据库中的表 我看不出我的代码中有任何错误,但它不起作用 有人能睁

我想知道为什么这个代码不起作用;它总是返回零(任何受影响的行)。该数据库是Access数据库。我可以插入和删除数据,但不能更新数据。这是一个Windows Forms C#应用程序

注意:在CRUD方法中,我只填充参数并执行sqlcommand

int retorno = command.ExecuteNonQuery();
return retorno;
我验证了所有参数、字段名称和命令执行时没有错误,但始终返回零,并且不更新数据库中的表

我看不出我的代码中有任何错误,但它不起作用


有人能睁开我的眼睛吗?

试试这个。参数的顺序应与sql中的相同。在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;
    }

试试这个。参数的顺序应与sql中的相同。在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;
    }

试试这个。参数的顺序应与sql中的相同。在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;
    }

试试这个。参数的顺序应与sql中的相同。在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;
    }

我要感谢大家的回答

问题解决了:参数没有按使用顺序传递

我只是纠正了参数顺序:

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";

现在表格已正确更新。

我要感谢所有人的回答

问题解决了:参数没有按使用顺序传递

我只是纠正了参数顺序:

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";

现在表格已正确更新。

我要感谢所有人的回答

问题解决了:参数没有按使用顺序传递

我只是纠正了参数顺序:

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";

现在表格已正确更新。

我要感谢所有人的回答

问题解决了:参数没有按使用顺序传递

我只是纠正了参数顺序:

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";

现在表被正确地更新了。

我一直认为在查询中使用与名称(例如,id=@id)匹配的字典意味着在向OleDbCommand.parameters列表添加参数时,顺序并不重要。事实上,对于.NET和Oracle或SQL Server数据库,我从来没有遇到过这个问题。但是,当我在OleDbCommand.Parameters列表中将id参数(请参见下面的单行)作为第一个条目时,我发现msaccess(或者可能是OleDb)始终返回0。将行移动到参数列表的末尾(WHERE子句中使用了id)后,ExecuteOnQuery返回1(预期结果)


我一直认为,在查询中使用与名称(例如,id=@id)匹配的字典意味着在向OleDbCommand.parameters列表添加参数时,顺序并不重要。事实上,对于.NET和Oracle或SQL Server数据库,我从来没有遇到过这个问题。但是,当我在OleDbCommand.Parameters列表中将id参数(请参见下面的单行)作为第一个条目时,我发现msaccess(或者可能是OleDb)始终返回0。将行移动到参数列表的末尾(WHERE子句中使用了id)后,ExecuteOnQuery返回1(预期结果)


我一直认为,在查询中使用与名称(例如,id=@id)匹配的字典意味着在向OleDbCommand.parameters列表添加参数时,顺序并不重要。事实上,对于.NET和Oracle或SQL Server数据库,我从来没有遇到过这个问题。但是,当我在OleDbCommand.Parameters列表中将id参数(请参见下面的单行)作为第一个条目时,我发现msaccess(或者可能是OleDb)始终返回0。将行移动到参数列表的末尾(WHERE子句中使用了id)后,ExecuteOnQuery返回1(预期结果)


我一直认为,在查询中使用与名称(例如,id=@id)匹配的字典意味着在向OleDbCommand.parameters列表添加参数时,顺序并不重要。事实上,对于.NET和Oracle或SQL Server数据库,我从来没有遇到过这个问题。但是,当我在OleDbCommand.Parameters列表中将id参数(请参见下面的单行)作为第一个条目时,我发现msaccess(或者可能是OleDb)始终返回0。将行移动到参数列表的末尾(WHERE子句中使用了id)后,ExecuteOnQuery返回1(预期结果)


不确定access数据库。。连接字符串还是什么?它实际上是否有任何categorialId=@CatId的记录?您是否在数据库管理器中尝试过类似SSMS的查询?里面有更新吗?你调试代码了吗?添加参数值时,查询是什么样子的?什么是
AcessoDB
CRUD
?您正在使用OleDb.Net提供程序吗?如果使用OLE DB,它不支持命名参数。您需要按照在命令中添加它们的相同顺序提供它们。发布
AcessoDB.CRUD的代码,人们可能会帮助您。我们缺乏通过TCP/IP阅读您的想法来调试代码的能力。请不要这样做:
catch(Exception ex){throw ex;}
。使用
throw
无例外地保留其stacktrace。这可能与此相关-参数未按使用顺序传递。access数据库不确定。。连接字符串还是什么?它实际上是否有任何categorialId=@CatId的记录?您是否在数据库管理器中尝试过类似SSMS的查询?里面有更新吗?你调试代码了吗?添加参数值时,查询是什么样子的?什么是
AcessoDB
CRUD
?您正在使用OleDb.Net提供程序吗?如果使用OLE DB,它不支持命名参数。您需要以相同的方式提供它们