0

据我所知,所有参数都是绑定的......有什么想法吗?我试图找到明确的答案,并尝试将这些作为具有显式数据类型(当前代码)的参数添加,并且也只使用 .addwithvalue (注释掉的代码)。仍然得到相同的错误。

        public bool WriteToDatabase(OracleConnection dbConnection, string sSiteCode)
    {
        if (!dbConnection.IsConnected)
        {
            Debug.WriteLine("Could not update record - connection closed!");
            return false;
        }

        //string sSQL = "select * FROM PSDATA." + m_sSiteCode + " WHERE PItimedate>'"+ m_sOracleStartTime +"' AND PItimedate<'"+ m_sOracleEndTime + "'  ORDER BY pitimedate desc";

        string sSQL_TO_UPDATE_RECORD = "INSERT INTO PSDATA." + sSiteCode + " (PITIMEDATE, PITIME, PIDATE, SRC, EVC, SEV, VSA1, VSA2, VSA3, VSA4, VSA5, VSA6, VSA7, VSA8, VSA9, VSA10, VSA11, VSA12, VSA13, VSA14) ";
        sSQL_TO_UPDATE_RECORD += " VALUES (:PITIMEDATE, :PITIME, :PIDATE, :SRC, :EVC, :SEV, :VSA1, :VSA2, :VSA3, :VSA4, :VSA5, :VSA6, :VSA7, :VSA8, :VSA9, :VSA10, :VSA11, :VSA12, :VSA13, :VSA14) ";

        //Set up the command
        OleDbCommand oCommand = new OleDbCommand(sSQL_TO_UPDATE_RECORD, dbConnection.ConnectionObject);
        oCommand.Connection = dbConnection.ConnectionObject;


        //Now add parameter values
        if (m_dtPIDateTime == null) { oCommand.Parameters.Add(new OleDbParameter(":PITIMEDATE", OleDbType.DBDate) {Value = DBNull.Value}); } else {oCommand.Parameters.Add(new OleDbParameter(":PITIMEDATE", OleDbType.DBDate) {Value =m_dtPIDateTime});};
        if (m_sPITime == null) { oCommand.Parameters.Add(new OleDbParameter(":PITIME", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":PITIME", OleDbType.Char) { Value = m_sPITime }); };
        if (m_sPIDate == null) { oCommand.Parameters.Add(new OleDbParameter(":PIDATE", OleDbType.DBDate) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":PIDATE", OleDbType.DBDate) { Value = m_dtPIDate }); };

        if (m_SRC== null) { oCommand.Parameters.Add(new OleDbParameter(":SRC", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":SRC", OleDbType.Char) { Value = m_SRC }); };
        if (m_EVC== null) { oCommand.Parameters.Add(new OleDbParameter(":EVC", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":EVC", OleDbType.Char) { Value = m_EVC }); };
        if (m_SEV == null) { oCommand.Parameters.Add(new OleDbParameter(":SEV", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":SEV", OleDbType.Char) { Value = m_SEV}); };

        if (m_VSA1== null) { oCommand.Parameters.Add(new OleDbParameter(":VSA1", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA1", OleDbType.Char) { Value = m_VSA1}); };
        if (m_VSA2== null) { oCommand.Parameters.Add(new OleDbParameter(":VSA2", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA2", OleDbType.Char) { Value = m_VSA2 }); };
        if (m_VSA3 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA3", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA3", OleDbType.Char) { Value = m_VSA3 }); };
        if (m_VSA4 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA4", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA4", OleDbType.Char) { Value = m_VSA4 }); };
        if (m_VSA5 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA5", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA5", OleDbType.Char) { Value = m_VSA5 }); };
        if (m_VSA6 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA6", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA6", OleDbType.Char) { Value = m_VSA6 }); };
        if (m_VSA7 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA7", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA7", OleDbType.Char) { Value = m_VSA7 }); };
        if (m_VSA8 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA8", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA8", OleDbType.Char) { Value = m_VSA8 }); };
        if (m_VSA9 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA9", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA9", OleDbType.Char) { Value = m_VSA9 }); };
        if (m_VSA10 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA10", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA10", OleDbType.Char) { Value = m_VSA10 }); };
        if (m_VSA11== null) { oCommand.Parameters.Add(new OleDbParameter(":VSA11", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA11", OleDbType.Char) { Value = m_VSA11 }); };
        if (m_VSA12 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA12", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA12", OleDbType.Char) { Value = m_VSA12 }); };
        if (m_VSA13 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA13", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA13", OleDbType.Char) { Value = m_VSA13 }); };
        if (m_VSA14 == null) { oCommand.Parameters.Add(new OleDbParameter(":VSA14", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":VSA14", OleDbType.Char) { Value = m_VSA14 }); };

        //oCommand.Prepare();
        /*
        if (m_dtPIDateTime == null) { oCommand.Parameters.AddWithValue(":PITIMEDATE", DBNull.Value); } else { oCommand.Parameters.AddWithValue(":PITIMEDATE", m_dtPIDateTime.ToOADate()); };
        //if (m_dtPIDateTime == null) { oCommand.Parameters.AddWithValue(":PITIMEDATE", DBNull.Value); } else { oCommand.Parameters.AddWithValue(":PITIMEDATE", DBNull.Value); };
        if (m_sPITime == "") { oCommand.Parameters.AddWithValue(":PITIME", DBNull.Value); } else { oCommand.Parameters.AddWithValue(":PITIME", m_sPITime); };
        if (m_sPIDate == "") { oCommand.Parameters.AddWithValue(":PIDATE", DBNull.Value); } else { oCommand.Parameters.AddWithValue(":PIDATE", m_sPIDate); };
        if (m_SRC == "") { oCommand.Parameters.AddWithValue(":SRC", "."); } else { oCommand.Parameters.AddWithValue(":SRC", m_SRC); };
        if (m_EVC == "") { oCommand.Parameters.AddWithValue(":EVC", "."); } else { oCommand.Parameters.AddWithValue(":EVC", m_EVC); };
        if (m_SEV == "") { oCommand.Parameters.AddWithValue(":SEV", "."); } else { oCommand.Parameters.AddWithValue(":SEV", m_SEV); };
        if (m_VSA1 == "") { oCommand.Parameters.AddWithValue(":VSA1", "."); } else { oCommand.Parameters.AddWithValue(":VSA1", m_VSA1); };
        if (m_VSA2 == "") { oCommand.Parameters.AddWithValue(":VSA2", "."); } else { oCommand.Parameters.AddWithValue(":VSA2", m_VSA2); };
        if (m_VSA3 == "") { oCommand.Parameters.AddWithValue(":VSA3", "."); } else { oCommand.Parameters.AddWithValue(":VSA3", m_VSA3); };
        if (m_VSA4 == "") { oCommand.Parameters.AddWithValue(":VSA4", "."); } else { oCommand.Parameters.AddWithValue(":VSA4", m_VSA4); };
        if (m_VSA5 == "") { oCommand.Parameters.AddWithValue(":VSA5", "."); } else { oCommand.Parameters.AddWithValue(":VSA5", m_VSA5); };
        if (m_VSA6 == "") { oCommand.Parameters.AddWithValue(":VSA6", "."); } else { oCommand.Parameters.AddWithValue(":VSA6", m_VSA6); };
        if (m_VSA7 == "") { oCommand.Parameters.AddWithValue(":VSA7", "."); } else { oCommand.Parameters.AddWithValue(":VSA7", m_VSA7); };
        if (m_VSA8 == "") { oCommand.Parameters.AddWithValue(":VSA8", "."); } else { oCommand.Parameters.AddWithValue(":VSA8", m_VSA8); };
        if (m_VSA9 == "") { oCommand.Parameters.AddWithValue(":VSA9", "."); } else { oCommand.Parameters.AddWithValue(":VSA9", m_VSA9); };
        if (m_VSA10 == "") { oCommand.Parameters.AddWithValue(":VSA10", "."); } else { oCommand.Parameters.AddWithValue(":VSA10", m_VSA10); };
        if (m_VSA11 == "") { oCommand.Parameters.AddWithValue(":VSA11", "."); } else { oCommand.Parameters.AddWithValue(":VSA11", m_VSA11); };
        if (m_VSA12 == "") { oCommand.Parameters.AddWithValue(":VSA12", "."); } else { oCommand.Parameters.AddWithValue(":VSA12", m_VSA12); };
        if (m_VSA13 == "") { oCommand.Parameters.AddWithValue(":VSA13", "."); } else { oCommand.Parameters.AddWithValue(":VSA13", m_VSA13); };
        if (m_VSA14 == "") { oCommand.Parameters.AddWithValue(":VSA14", "."); } else { oCommand.Parameters.AddWithValue(":VSA14", m_VSA14); };
        */
        //Now execute the query
        Debug.WriteLine(oCommand.CommandText);
        foreach (OleDbParameter oParam in oCommand.Parameters)
        {
            Debug.WriteLine(oParam.ParameterName + " " + oParam.Value.ToString());
        }
        //oCommand.Prepare();
        int nRowUpdated = oCommand.ExecuteNonQuery();
        return (nRowUpdated == 1);

    }

请注意,我注释掉了 oCommand.Prepare() 因为我显然需要明确设置字段长度才能使用它!

4

1 回答 1

0

检查以下参数

if (m_sPITime == null) { oCommand.Parameters.Add(new OleDbParameter(":PITIME", OleDbType.Char) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":PITIME", OleDbType.Char) { Value = m_sPITime }); };
if (m_sPIDate == null) { oCommand.Parameters.Add(new OleDbParameter(":PIDATE", OleDbType.DBDate) { Value = DBNull.Value }); } else { oCommand.Parameters.Add(new OleDbParameter(":PIDATE", OleDbType.DBDate) { Value = m_dtPIDate }); };
  1. 如果m_sPITime是,DateTime那么您需要更改类型OleDbType.Char
  2. 两个条件检查相同的条件(m_sPIDate == null),您可能需要将第二个更改为if (m_dtPIDate == null)

您还可以通过使用??下面的运算符来减少 if else 语句

 oCommand.Parameters.Add(new OleDbParameter(":PIDATE", OleDbType.DBDate) { Value = m_dtPIDate?? DBNull.Value });

对于字符串类型

oCommand.Parameters.Add(new OleDbParameter(":PITIME", OleDbType.Char) { Value =(m_sPITime==null?(object)DBNull.Value:m_sPITime) }); 
于 2013-09-26T03:05:05.670 回答