使用参数化查询 sql parameter
insert 时,我的一个DateTime类型的字段想传入一个null值,但会报错“参数化查询 需要参数 但未提供该参数”
高手帮忙~~

解决方案 »

  1.   

    数据库字段是不是not null,还有要是传入null的话  建议还是在数据库里面设置默认值是null,不然很容易出错····
      

  2.   

     public int SaveData(TaskManageInfo taskManage)
            {
                int index = 0;
                string strSQL = null;            if (taskManage.ID == 0)
                {
                    strSQL = "INSERT INTO TaskManage(ID,Name,Project,Block,Type,Description,Res,PersonInCharge,RegisterPerson,State,PlanToFinish,IsMailRemind,IsPublic,IsUndetermined,FinishTime,FinishReport) ";
                    strSQL += "VALUES(@ID,@Name,@Project,@Block,@Type,@Description,@Res,@PersonInCharge,@RegisterPerson,@State,@PlanToFinish,@IsMailRemind,@IsPublic,@IsUndetermined,@FinishTime,@FinishReport)";
                    taskManage.ID = DBHelper.GetMaxID("TaskManage");
                }
                else
                {
                    strSQL = "UPDATE Student SET ID = @ID,Name = @Name,Project = @Project,Block = @Block,Type = @Type,Description = @Description,Res = @Res,PersonInCharge = @PersonInCharge,RegisterPerson = @RegisterPerson,State = @State,PlanToFinish = @PlanToFinish,IsMailRemind = @IsMailRemind,IsPublic = @IsPublic,IsUndetermined = @IsUndetermined,FinishTime = @FinishTime,FinishReport = @FinishReport WHERE ID=@ID;";
                }            SqlParameter[] sqlParameter = GetParameters();
                sqlParameter[index++].Value = taskManage.ID;
                sqlParameter[index++].Value = taskManage.Name;
                sqlParameter[index++].Value = taskManage.Project;
                sqlParameter[index++].Value = taskManage.Block;
                sqlParameter[index++].Value = taskManage.Type;
                sqlParameter[index++].Value = taskManage.Description;
                sqlParameter[index++].Value = taskManage.Res;
                sqlParameter[index++].Value = taskManage.PersonInCharge;
                sqlParameter[index++].Value = taskManage.RegisterPerson;
                sqlParameter[index++].Value = taskManage.State;
                sqlParameter[index++].Value = taskManage.PlanToFinish;
                sqlParameter[index++].Value = taskManage.IsMailRemind;
                sqlParameter[index++].Value = taskManage.IsPublic;
                sqlParameter[index++].Value = taskManage.IsUndetermined;
                sqlParameter[index++].Value = taskManage.FinishTime;
                sqlParameter[index++].Value = taskManage.FinishReport;            try
                {
                    DBHelper.ExecuteNonQuery(strSQL, sqlParameter);
                }
                catch(Exception ex)
                {
                    throw (ex);
                }
                return taskManage.ID.Value;        }//-----------------------------------------------------------------------------------
            /// <summary>
            /// 初始化参数
            /// </summary>
            /// <returns></returns>
            private SqlParameter[] GetParameters()
            {
                SqlParameter[] parms;            parms = new SqlParameter[] {
                      new SqlParameter("@ID",SqlDbType.Int),
                      new SqlParameter("@Name",SqlDbType.NVarChar,50),
                      new SqlParameter("@Project",SqlDbType.NVarChar,50),
                      new SqlParameter("@Block",SqlDbType.NVarChar,50),
                      new SqlParameter("@Type",SqlDbType.NVarChar,50),
                      new SqlParameter("@Description",SqlDbType.NVarChar,500),
                      new SqlParameter("@Res",SqlDbType.NVarChar,500),
                      new SqlParameter("@PersonInCharge",SqlDbType.NVarChar,50),
                      new SqlParameter("@RegisterPerson",SqlDbType.NVarChar,50),
                      new SqlParameter("@State",SqlDbType.Int),
                      new SqlParameter("@PlanToFinish",SqlDbType.DateTime,8,""),
                      new SqlParameter("@IsMailRemind",SqlDbType.Int),
                      new SqlParameter("@IsPublic",SqlDbType.Int),
                      new SqlParameter("@IsUndetermined",SqlDbType.Int),
                      new SqlParameter("@FinishTime",SqlDbType.DateTime),
                      new SqlParameter("@FinishReport",SqlDbType.NVarChar,500),
              };
                return parms;
            }//-------------------------------------------------------------------------------
     protected void btnSave_Click(object sender, EventArgs e)
        {
            TaskManageBO taskManageBO = new TaskManageBO();
            try
            {
                int isSuccess = taskManageBO.Save(SetInfo());
                if (isSuccess == 1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script>alert('保存成功!')</script>");
                }
            }
            catch(Exception ex)
            {
                string str = ex.Message.Replace("'", "\\'");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script>alert('" + str + "');</script>");
            }
        }
        
        //---------------封装实体------------------
        private TaskManageInfo SetInfo()
        {
            DateTime? nullDateTime = null;
            TaskManageInfo info = new TaskManageInfo();
            info.ID = int.Parse(hidID.Value);
            info.Name = this.txtName.Text;//任务名称
            info.Project = this.txtProject.Text;//所属项目
            info.Block = this.txtBlock.Text;//所属模块
            info.Type = this.txtType.Text;//任务类型
            info.Description = this.txtDescription.Text;//任务描述
            info.PlanToFinish = txtPlanToFinish.Text == "" ? nullDateTime : DateTime.Parse(txtPlanToFinish.Text); //计划完成时间
            info.PersonInCharge = txtPersonInCharge.Text; //责任人
            info.Res = txtRes.Text; //备注
            info.IsMailRemind = this.chbMail.Checked == true ? 1 : 0; //是否邮件提醒
            info.IsPublic = this.chbIsPublic.Checked == true ? 1 : 0; //是否公开任务
            info.IsUndetermined = null;//计划完成时间(是否待写) 暂时不用此功能
            info.State = 0;//任务状态,默认为“处理中”
            info.RegisterPerson = this.userName; //登记人
            info.FinishReport = "";//结束报告,任务结束时赋值,这里先赋空值
            info.FinishTime = null;//结束时间
            return info;
        }
      

  3.   

    贴出代码看看
    parameters[1].Value = model.A?? DBNull.Value;
      

  4.   

    数据库字段是不是not null,还有要是传入null的话 建议还是在数据库里面设置默认值是null,不然很容易出错····
      

  5.   

    我定义了可空的DateTime类型,也出现了类似的问题。parameters[1].Value = model.A?? DBNull.Value;但是这个不行。怎么解决?