使用Oracle更新命令C#

问题描述 投票:-1回答:2

我有一个问题需要更新我的数据库。

public void UpdateAto(AtoEntity atoEntity)
{
    OracleDataAdapter da = new OracleDataAdapter();

    string oradb = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=blabla.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=test13)));;User Id=id13;Password=pwd13;";
    OracleConnection con = new OracleConnection(oradb);
    con.Open();

    OracleCommand cmd = new OracleCommand();
    cmd.Connection = con;

    cmd.CommandText = 
        "UPDATE SLAPTEOP SET XOCL ='" + atoEntity.OidOcl +
                         "',NOM='" + atoEntity.NameAto +
                         "',ETAT='-1" +
                         "',NBPAAN='11" +
                         "',DESCRIPTION='" + atoEntity.DescriptionAto +
                         "',APPORTEUR='" + atoEntity.ContributorAto +
                         "',AUTEUR_MISEAJOUR='" + atoEntity.AuthorUpdateAto +
                          "',DATE_MISEAJOUR=SYSDATE WHERE OID ='" + atoEntity.OidAto + "'";

    cmd.CommandType = CommandType.Text;

    cmd.ExecuteNonQuery();
    con.Dispose();
}

请求很好,它是在没有参数的情况下编写的,以使其更简单。它已经在SGBD上进行了测试。选择和插入请求也适用。

当我执行cmd.ExecuteNonQuery()时,数据库中没有任何反应。它转入循环。

添加了try / catch块但未捕获任何错误或异常。

c# database oracle sql-update command
2个回答
0
投票

您应该使用绑定变量。检查更新行的数量,如下所示:

cmd.CommandType = CommandType.Text;
cmd.CommandText = 
   "UPDATE SLAPTEOP SET XOCL = :xocl, 
       NOM = :mom, ETAT = -1, 
       NBPAAN = 11, DESCRIPTION = :desc, 
       APPORTEUR = :apporteur, AUTEUR_MISEAJOUR = :auteur, 
       DATE_MISEAJOUR = SYSDATE
    WHERE OID = :oid";

cmd.Parameters.Add("xocl", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.OidOcl;
cmd.Parameters.Add("mom", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.NameAto;
cmd.Parameters.Add("desc", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.DescriptionAto;
cmd.Parameters.Add("apporteur", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.ContributorAto;
cmd.Parameters.Add("auteur", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.AuthorUpdateAto;
cmd.Parameters.Add("oid", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.OidAto;

var r = cmd.ExecuteNonQuery();

Console.WriteLine("{0} rows updated", r);

0
投票

拖放我没有看到问题......

   cmd.CommandType = CommandType.Text;
            cmd.CommandText = 
                "UPDATE SLAPTEOP SET XOCL = :xocl,"+ 
            "NOM = :nom, ETAT = -1,"+
            "NBPAAN = 11, DESCRIPTION = :desc,"+ 
            "APPORTEUR = :apporteur, AUTEUR_MISEAJOUR = :auteur,"+
            "DATE_MISEAJOUR = SYSDATE"+
            "WHERE OID = :oid";

            cmd.Parameters.Add("xocl", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.OidOcl;
            cmd.Parameters.Add("nom", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.NameAto;
            cmd.Parameters.Add("desc", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.DescriptionAto;
            cmd.Parameters.Add("apporteur", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.ContributorAto;
            cmd.Parameters.Add("auteur", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.AuthorUpdateAto;
            cmd.Parameters.Add("oid", OracleDbType.Varchar2, ParameterDirection.Input).Value = atoEntity.OidAto;
热门问题
推荐问题
最新问题