我的textBox.Text='' 是等于空了 sql语句是这样 
update a set aa=textBox.Text from a这样插入到数据库里面,字段aa 的值是' '了,不是null,这样就不对了,如果没有数据,字段值必须为null而不是' '。我想到一个办法:
 if(textBox.Text='' ){sql=update a set aa=null from a}else{sql=update a set aa=textBox.Text from a}这样是可行的,但是因为sql语句很多 改起来很不方便 ,所以请求大神给指明一下思路。Ps. 我说过把textBox.Text设置为null,就是textBox.Text=null,插入数据库以后还是' '。

解决方案 »

  1.   

    DBNull
      

  2.   

    我也发过一个帖子,你可以参考一下http://bbs.csdn.net/topics/392259277
      

  3.   

    参见ExtensionMethods:https://stackoverflow.com/questions/170186/set-a-database-value-to-null-with-a-sqlcommand-parameters
      

  4.   

    纯拼不好,还是参数代为上: public static string TextorNull(this TextBox textBox)
            {
                 return string.IsNullOrEmpty(textBox.Text)?"null":textBox.Text;
            }
    使用:"update a set aa="+textBox.TextorNull()+" from a"
      

  5.   

                string name = txtName.Text;
                string age = txtAge.Text;
                string height = txtHeight.Text;
                object objName;
                if (name.Length <= 0)
                {
                    objName = DBNull.Value;
                }
                else
                {
                    objName = name;
                }
                object objAge;
                if (age.Length <= 0)
                {
                    objAge = DBNull.Value;
                }
                else
                {
                    objAge = age;
                }
                object objHeight;
                if (height.Length <= 0)
                {
                    objHeight = DBNull.Value;
                }
                else
                {
                    objHeight = height;
                }
                SqlHelper.ExecuteNonQuery(@"insert into T_Person(Name,Age,Height) values (@Name,@Age,@Height)",
                    new SqlParameter("@Name", objName),
                    new SqlParameter("@Age", objAge),
                    new SqlParameter("@Height", objHeight));可以这么使用DBNULL