Insert oralce database data with parameters

Sometimes without using the ORM framework, when the insert statement with parameters is written manually or generated by the code generator, such as

///  <summary> 
        /// Add a piece of data
         ///  </summary> 
        public  static  bool Add(Model.IMGINFO model)
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("insert into IMGINFO(");
            strSql.Append("ID,RYCRJLXXID,DSP,XP,INSERTTIME,SCIP)");
            strSql.Append(" values (");
            strSql.Append(":ID,:RYCRJLXXID,:DSP,:XP,:INSERTTIME,:SCIP)");
            OracleParameter[] parameters = {
                    new OracleParameter(":ID", OracleType.VarChar,32),
                    new OracleParameter(":RYCRJLXXID", OracleType.VarChar,32),
                    new OracleParameter(":DSP", OracleType.Blob),
                    new OracleParameter(":XP", OracleType.Blob),
                    new OracleParameter(":INSERTTIME", OracleType.DateTime),
                    new OracleParameter(":SCIP", OracleType.VarChar,32)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.RYCRJLXXID;
            parameters[2].Value = model.DSP;
            parameters[3].Value = model.XP;
            parameters[4].Value = model.INSERTTIME;
            parameters[5].Value = model.SCIP;

            return DBHelperTarget.ExecuteCommand(strSql.ToString(),parameters);
        
        }

At first glance, there is no problem with this code. The parameters are bound, but an error is reported when executing. The error probably means that some parameters have no bound data. How can this happen?

Because the DSP, XP and INSERTTIME of the object in this example are all non-required items, the problem lies here. Sometimes these fields are not assigned when assigning an object, which is the default null, but when Oracle executes, it will think that You are not binding a value to the parameter. Here is a solution to define a nullable non-string type field as object type.

E.g

[Serializable]
    public partial class IMGINFO
    {
        public IMGINFO()
        {}
        #region Model
        private string _id;
        private string _rycrjlxxid;
        private object _dsp;
        private object _xp;
        private object _inserttime;

        public string SCIP { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ID
        {
            set{ _id=value;}
            get{return _id;}
        }

        public string RYCRJLXXID
        {
            set{ _rycrjlxxid=value;}
            get{return _rycrjlxxid;}
        }
 
        public object DSP
        {
            set{ _dsp=value;}
            get{return _dsp;}
        }

        public object XP
        {
            set{ _xp=value;}
            get{return _xp;}
        }
   
        public object INSERTTIME
        {
            set{ _inserttime=value;}
            get{return _inserttime;}
        }

 

When assigning values ​​to the object, if these special fields are empty, assign the value to DBNull.Value, and execute it again, and find that no error is reported.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325298267&siteId=291194637