在字节类型字段中插入null时,字符串或缓冲区长度无效

时间:2013-02-13 10:22:02

标签: c# asp.net null byte informix

插入时出现以下异常:

ERROR [HY090] [Informix .NET provider]Invalid string or buffer length.

何时

obj.Request_file = null;//of byte[] type

          using (IfxConnection con = new IfxConnection(ConfigurationSettings.AppSettings["con1"].ToString()))
            {
                con.Open();
                StringBuilder cmdTxt = new StringBuilder();

                cmdTxt.Append(" INSERT INTO request_data(req_ser, req_year, req_date, req_emp_type, req_emp_num, req_emp_name, person_type,person_num,person_name,about_person,other_description,main_code , year,req_desc, login_num , req_title,request_file) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?) ");

                int MaxFollowSerial = Get_Max_FollowSerial(int.Parse(obj.RequestYear.ToString())) + 1;
                IfxCommand myIfxCmd = new IfxCommand(cmdTxt.ToString(), con);

                myIfxCmd.CommandType = CommandType.Text;
                myIfxCmd.Parameters.Clear();

                myIfxCmd.Parameters.Add("req_ser", MaxFollowSerial.ToString());
                myIfxCmd.Parameters.Add("req_year", obj.RequestYear.ToString());
                myIfxCmd.Parameters.Add("req_date", obj.RequestDate.ToString().Split(' ')[0]);
                myIfxCmd.Parameters.Add("req_emp_type", obj.RequestEmpType.ToString().Trim());
                myIfxCmd.Parameters.Add("req_emp_num", obj.RequestEmpNum.ToString().Trim());
                myIfxCmd.Parameters.Add("req_emp_name", obj.RequestEmpName.ToString().Trim());
                myIfxCmd.Parameters.Add("person_type", obj.PersonType.ToString().Trim());
                myIfxCmd.Parameters.Add("person_num", obj.PersonNum.ToString().Trim());
                myIfxCmd.Parameters.Add("person_name", obj.PersonName.ToString().Trim());
                myIfxCmd.Parameters.Add("about_person", obj.AboutPerson.ToString().Trim());
                myIfxCmd.Parameters.Add("other_description", obj.OtherDescription.ToString().Trim());
                myIfxCmd.Parameters.Add("main_code", obj.MainCode.ToString());
                myIfxCmd.Parameters.Add("year", obj.Year.ToString());
                myIfxCmd.Parameters.Add("req_desc", obj.RequestDescription.ToString().Trim());
                myIfxCmd.Parameters.Add("login_num", obj.LoginNum.ToString());
                myIfxCmd.Parameters.Add("req_title", obj.Title.ToString());
                myIfxCmd.Parameters.Add("request_file", obj.Request_file);
                int affectedRow = myIfxCmd.ExecuteNonQuery();
                con.Close();
                con.Dispose();
                if (affectedRow > 0)
                    return MaxFollowSerial;
                else
                    return affectedRow;
            }

1 个答案:

答案 0 :(得分:3)

试试这个:

if(obj.Request_file != null)
    myIfxCmd.Parameters.Add("request_file", obj.Request_file);
else
    myIfxCmd.Parameters.Add("request_file", DBNull.Value);