将绑定参数传递给PL SQL更新语句

时间:2018-04-24 15:59:59

标签: c# sql dml

我已经使用SELECT语句多次完成此操作而没有任何问题。我也理解DML PL SQL语句不允许在"某些"中使用绑定参数。实例,但我已经获得UPDATE语句。它绝不是包含在内的。我不知道还有什么可以处理我的陈述。下面我已经参与了处理UPDATE语句的部分代码。我基本上将参数传递给我的语句(特别是sampleID和testID变量都被声明为整数),但语句在变量的位置包含零。这意味着它无法识别已分配给sampleID和testID变量的值。我不明白我在这里做错了什么。第一个更新命令(ucommand)可以工作,但最后两个命令不起作用。我迷路了。

if(!row["field_result"].ToString().Equals(""))
{
    //Build SQL commands to address the field result
    string ucommand = "UPDATE CC_MSD_LIMS_STAGING SET FIELD_RESULT = '" + @locfield + "', CREATEDDT = SYSDATE"
        + " WHERE MONITORING_ID = " + @locmon_ID + " AND IPACS_SAMPLE_ID = " + @locipacs_ID + " AND FACILITY_ID = "
        + @locfacil_ID + " AND POLLUTANT = '" + @locpoll + "'";
    string idcmd = "SELECT SAMPLEID FROM LIM_SAMPLE WHERE SAMPLENUMBER = (SELECT PRIM_SAM_KEY FROM CC_MSD_LIMS_STAGING "
        + "WHERE MONITORING_ID = " + @locmon_ID + " AND IPACS_SAMPLE_ID = " + @locipacs_ID + " AND FACILITY_ID = "
        + @locfacil_ID + " AND POLLUTANT = '" + @locpoll + "')";
    string testcmd = "SELECT TESTID FROM LIM_TESTGROUPTESTS WHERE TESTGROUPID = (SELECT TESTGROUPID FROM LIM_TESTGROUP WHERE "
        + "TESTGROUPNAME = '" + @locpoll + "' AND ACTIVE = 1 AND PUBLISHED = 1)";
    string upd1cmd = "UPDATE LIM_SAMPLETESTRUNRESULT SET SAMPLETESTRUNRESULTRAW = '" + @locfield + "', SAMPLETESTRUNRESULTCALC "
        + "= '" + @locfield + "', SAMPLETESTRUNRESULTREPORT = '" + @locfield + "', SAMPLETESTRUNRESULTSTATUS = 3, EDITEDDT "
        + "= SYSDATE, EDITEDBY = 128 WHERE SAMPLEID = " + @sampleID + " AND TESTID = " + @testID;
    string upd2cmd = "UPDATE LIM_SAMPLETESTRESULTPROCESSES SET PROCESSSTATUS = 3.3, PROCESSCOMPLETEDT = SYSDATE, PROCESSCOMPLETEBY "
        + "= 128 WHERE SAMPLEID = " + @sampleID + " AND TESTID = " + @testID;

    //Execute commands
    try
    {
        using (OracleCommand cmd = new OracleCommand(ucommand, conn))
        {
            cmd.Parameters.Add("@locmon_ID", locmon_ID);
            cmd.Parameters.Add("@locfacil_ID", locfacil_ID);
            cmd.Parameters.Add("@locipacs_ID", locipacs_ID);
            cmd.Parameters.Add("@locpoll", locpoll);
            cmd.Parameters.Add("@locfield", locfield);
            cmd.ExecuteNonQuery();
            cmd.CommandText = "COMMIT WORK";
            cmd.ExecuteNonQuery();
            cmd.CommandText = idcmd;
            sampleID = int.Parse(cmd.ExecuteScalar().ToString());
            cmd.CommandText = testcmd;
            testID = int.Parse(cmd.ExecuteScalar().ToString());
        }

        using (OracleCommand cmd1 = new OracleCommand(upd1cmd, conn))
        {
            cmd1.Parameters.Add("@sampleID", sampleID);
            cmd1.Parameters.Add("@testID", testID);
            cmd1.CommandText = upd1cmd;
            cmd1.ExecuteNonQuery();
            cmd1.CommandText = "COMMIT WORK";
            cmd1.ExecuteNonQuery();
            cmd1.CommandText = upd2cmd;
            cmd1.ExecuteNonQuery();
            cmd1.CommandText = "COMMIT WORK";
            cmd1.ExecuteNonQuery();
        }

    }
    catch (Exception f)
    {
        //Log.Error("Problems with the field results.");
    }
}

the command that is passed to Oracle (notice the zeros)

1 个答案:

答案 0 :(得分:0)

在弄乱了这个之后,我发现我真的不需要传递我最后两个更新语句的参数。我刚刚根据已经收集的值构建了字符串语句。所以我重新创建了代码块,如下所示,它有效,感谢@Steve的时间:

if (!row["field_result"].ToString().Equals(""))
                                {
                                    //Build SQL commands to address the field result
                                    string ucommand = "UPDATE CC_MSD_LIMS_STAGING SET FIELD_RESULT = '" + @locfield + "', CREATEDDT = SYSDATE"
                                        + " WHERE MONITORING_ID = " + @locmon_ID + " AND IPACS_SAMPLE_ID = " + @locipacs_ID + " AND FACILITY_ID = "
                                        + @locfacil_ID + " AND POLLUTANT = '" + @locpoll + "'";
                                    string idcmd = "SELECT SAMPLEID FROM LIM_SAMPLE WHERE SAMPLENUMBER = (SELECT PRIM_SAM_KEY FROM CC_MSD_LIMS_STAGING "
                                        + "WHERE MONITORING_ID = " + @locmon_ID + " AND IPACS_SAMPLE_ID = " + @locipacs_ID + " AND FACILITY_ID = "
                                        + @locfacil_ID + " AND POLLUTANT = '" + @locpoll + "')";
                                    string testcmd = "SELECT TESTID FROM LIM_TESTGROUPTESTS WHERE TESTGROUPID = (SELECT TESTGROUPID FROM LIM_TESTGROUP WHERE "
                                        + "TESTGROUPNAME = '" + @locpoll + "' AND ACTIVE = 1 AND PUBLISHED = 1)"; 

                                    //Execute commands
                                    try
                                    {
                                        using (OracleCommand cmd = new OracleCommand(ucommand, conn))
                                        {
                                            cmd.Parameters.Add("@locmon_ID", locmon_ID);
                                            cmd.Parameters.Add("@locfacil_ID", locfacil_ID);
                                            cmd.Parameters.Add("@locipacs_ID", locipacs_ID);
                                            cmd.Parameters.Add("@locpoll", locpoll);
                                            cmd.Parameters.Add("@locfield", locfield);
                                            cmd.ExecuteNonQuery();
                                            cmd.CommandText = "COMMIT WORK";
                                            cmd.ExecuteNonQuery();
                                            cmd.CommandText = idcmd;
                                            sampleID = int.Parse(cmd.ExecuteScalar().ToString());
                                            cmd.CommandText = testcmd;
                                            testID = int.Parse(cmd.ExecuteScalar().ToString());
                                        }

                                        string upd1cmd = "";

                                        using (OracleCommand cmd1 = new OracleCommand(upd1cmd,conn))
                                        {
                                            upd1cmd = "UPDATE LIM_SAMPLETESTRUNRESULT SET SAMPLETESTRUNRESULTRAW = '" + locfield + "', SAMPLETESTRUNRESULTCALC "
                                            + "= '" + locfield + "', SAMPLETESTRUNRESULTREPORT = '" + locfield + "', SAMPLETESTRUNRESULTSTATUS = 3, EDITEDDT "
                                            + "= SYSDATE, EDITEDBY = 128 WHERE SAMPLEID = " + sampleID + " AND TESTID = " + testID + " AND RUNID = 1";

                                            string upd2cmd = "UPDATE LIM_SAMPLETESTRESULTPROCESSES SET PROCESSSTATUS = 3.3, PROCESSCOMPLETEDT = SYSDATE, PROCESSCOMPLETEBY "
                                            + "= 128 WHERE SAMPLEID = " + sampleID + " AND TESTID = " + testID + " AND RUNID = 1";

                                            cmd1.CommandText = upd1cmd;
                                            cmd1.ExecuteNonQuery();
                                            cmd1.CommandText = "COMMIT WORK";
                                            cmd1.ExecuteNonQuery();
                                            cmd1.CommandText = upd2cmd;
                                            cmd1.ExecuteNonQuery();
                                            cmd1.CommandText = "COMMIT WORK";
                                            cmd1.ExecuteNonQuery();
                                        }

                                    }
                                    catch (Exception f)
                                    {
                                        //Log.Error("Problems with the field results.");
                                    }
                                }