/*
以下代码调用UpdateInStore方法修改记录
但是我这样设计之后,我可空类型的字段的值为空的时候 在UpdateInStore方法中设置值为DBNull 但是这样设置了之后貌似就相当于这个字段为空了吧,所以update语句不会修改这个字段的值,是这样的吧? 但是我的本意是修改这个字段的值为空值 
我这样用可空类型是不对的吧?可空类型到底该咋用呢?像我这样用的话感觉还不如不用呢 变得这么麻烦了??郁闷。麻烦各位指点一下吧!
*/ int? number = null;
        int? prid = null;
        int? pinzhi = null;
        int? danwei = null;
        decimal? baojia = null;
        decimal? shoujia = null;
        bool isgood, isbr;
private void button4_Click(object sender, EventArgs e)
        {            if (cboxPinZhi.SelectedIndex > -1)
                pinzhi = Convert.ToInt32(cboxPinZhi.SelectedValue.ToString());
            if (!string.IsNullOrEmpty(txtISGNum.Text.Trim()))
                number = Convert.ToInt32(txtISGNum.Text.Trim());
            if (!string.IsNullOrEmpty(txtGY.Text.Trim()))
                prid = Convert.ToInt32(txtGY.Text.Trim());
            if (!string.IsNullOrEmpty(txtSouJia.Text.Trim()))
                shoujia = Convert.ToDecimal(txtSouJia.Text);
            if (!string.IsNullOrEmpty(txtGIPrice.Text.Trim()))
                baojia = Convert.ToDecimal(txtGIPrice.Text);            if (chkgood.Checked)
                isgood = true;
            else
                isgood = false;
            if (chkbr.Checked)
                isbr = true;
            else
                isbr = false;
            string style = "";
            if (cboxcolor.SelectedIndex > -1)
                style = cboxcolor.SelectedValue.ToString();
            if (cboxdanwei.SelectedIndex > -1)
                danwei =Convert.ToInt32(cboxdanwei.SelectedValue.ToString());
            int success = doperate.UpdateInStore(dropTextBox1.Text, prid, Convert.ToInt32(cboxSName.SelectedValue.ToString()), danwei,
                number, baojia, txtISRe.Text.Trim(), pinzhi, xj.Text.Trim(), dj.Text.Trim(), pj.Text.Trim(), gd.Text.Trim(), fz.Text.Trim(), shoujia, style, isbr, isgood,
                Convert.ToInt32(dgvISManage.Rows[dgvISManage.CurrentCell.RowIndex].Cells[19].Value.ToString()));
            if (success > 0)
            {
                MessageBox.Show("信息修改成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                frmISManage_Load(sender, e);
            }
            else
                MessageBox.Show("修改出错,未能修改成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);        }//以下代码是UpdateInStore的代码
public int UpdateInStore(string gname, int? pid, int gclass, int? gunit, int? num, decimal? price, string beizhu, int? pinzhi, string xiongjing, string dijing,
            string pengjing, string gaodu, string fenzhi, decimal? shoujia, string color, bool isbareroot, bool isgood, int iid)
        {
            SqlConnection sqlcon = datacon.getcon();
            SqlCommand sqlcom = new SqlCommand("proc_updateInStore", sqlcon);
            sqlcom.CommandType = CommandType.StoredProcedure;
            sqlcom.Parameters.Add("@gname", SqlDbType.VarChar, 50).Value = gname;
            if (pid.HasValue)
                sqlcom.Parameters.Add("@pid", SqlDbType.Int).Value = pid;
            else
                sqlcom.Parameters.Add("@pid", SqlDbType.Int).Value = System.DBNull.Value;            sqlcom.Parameters.Add("@gclass", SqlDbType.Int).Value = gclass;
            if(gunit.HasValue)
                sqlcom.Parameters.Add("@gunit", SqlDbType.Int).Value = gunit;
            else
                sqlcom.Parameters.Add("@gunit", SqlDbType.Int).Value = System.DBNull.Value;
            if (num.HasValue)
                sqlcom.Parameters.Add("@num", SqlDbType.Int).Value = num;
            else
                sqlcom.Parameters.Add("@num", SqlDbType.Int).Value = System.DBNull.Value;
            if (price.HasValue)                sqlcom.Parameters.Add("@price", SqlDbType.Decimal).Value = price;
            else
                sqlcom.Parameters.Add("@price", SqlDbType.Decimal).Value = System.DBNull.Value;
            sqlcom.Parameters.Add("@beizhu", SqlDbType.VarChar, 1000).Value = beizhu;
            if (pinzhi.HasValue)
                sqlcom.Parameters.Add("@pinzhi", SqlDbType.Int).Value = pinzhi;
            else
                sqlcom.Parameters.Add("@pinzhi", SqlDbType.Int).Value = System.DBNull.Value;
            sqlcom.Parameters.Add("@xiongjing", SqlDbType.VarChar, 50).Value = xiongjing;
            sqlcom.Parameters.Add("@dijing", SqlDbType.VarChar, 50).Value = dijing;
            sqlcom.Parameters.Add("@pengjing", SqlDbType.VarChar, 50).Value = pengjing;
            sqlcom.Parameters.Add("@gaodu", SqlDbType.VarChar, 50).Value = gaodu;
            sqlcom.Parameters.Add("@fenzhi", SqlDbType.VarChar, 50).Value = fenzhi;
            if (shoujia.HasValue)                sqlcom.Parameters.Add("@shoujia", SqlDbType.Decimal).Value = shoujia;
            else
                sqlcom.Parameters.Add("@shoujia", SqlDbType.Decimal).Value = System.DBNull.Value;            sqlcom.Parameters.Add("@color", SqlDbType.VarChar, 20).Value = color;
            sqlcom.Parameters.Add("@isbareroot", SqlDbType.Bit).Value = isbareroot;
            sqlcom.Parameters.Add("@isgood", SqlDbType.Bit).Value = isgood;
            sqlcom.Parameters.Add("@iid", SqlDbType.Int).Value = iid;            sqlcon.Open();
            int success = 0;
            try
            {
                success = sqlcom.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sqlcom.Dispose();
                sqlcon.Close();
                sqlcon.Dispose();
            }
            return success;
        }

解决方案 »

  1.   

    在UpdateInStore方法中设置值为DBNull是对的,这样做会相应修改数据库中的值为空,不会跳过该字段的修改,永远不会。
      

  2.   

    可空类型和DBNull不能相互转换,这是.net设计时的历史遗留问题,是个遗憾,通常不要用可空类型,除非要用它为空的时候在系统中有意义
      

  3.   

    是否可为null应该是和数据模型有关,而不是用来标记更新不更新...
      

  4.   

    sqlcom.Parameters.Add("@shoujia", SqlDbType.Decimal).Value =shoujia.HasValue?shoujia:DBNull.value;
    或??