怎么往数据库的int字段写入空值呢?数据库的int字段可以为空
  我用了如下的方法:
                      object MulticastPort = null;
                        if (string.IsNullOrEmpty(this.textBox2.Text))
                        {
                            MulticastPort = DBNull.Value;
                        }
                        else
                        {
                            MulticastPort = this.textBox2.Text.ToString().Trim();
                        }
    string str = string.Format("INSERT INTO TSTV VALUES('{0}','{1}','{2}')", TSID, MulticastPort,ServerID);
用上面这个连接字符串将数据写入SQL,可是当textbox2为空时,这时MulticastPort写到数据库里的值总是零,我想让他为空,请问该怎么办呢
  

解决方案 »

  1.   

    string str = string.Format("INSERT INTO TSTV VALUES('{0}',null,'{2}')", TSID, MulticastPort,ServerID);
      

  2.   

    为空的 时候 你改下sql语句
      

  3.   

    不要上半部分的if...else之类的代码:
    直接如下:
    string str = string.Format("INSERT INTO TSTV VALUES('{0}','{1}','{2}')", TSID, textBox2.Text.Trim(),ServerID);
    str= str.Replace("''", "NULL");
      

  4.   

    DBNull.Value
    int? 可空类型
    SqlParameter[] sqlParams = new SqlParameter[] {
      new SqlParameter("@a", required),
      string.IsNullOrEmpty(a)? new SqlParameter("@a", DBNull.Value) : new SqlParameter("@a", a)
    };