c#有一个文本框为空,保存时如何实现数据库存入的是字符"NULL"

解决方案 »

  1.   

    插入的时候判断是否为空,如果为空的话插入DBNull.Value
      

  2.   

    比较笨的方法
    string strTEXT1=TEXT1.text.tostring;
    string strText2=Text2.text.tostring;//判断为空后需要存入‘NULL’的文本框if strText2.TEXT="";
    sql="insert into test values('"+strTEXT1+"','NULL')"
    ELSE
    sql="insert into test values('"+strTEXT1+"','"+strTEXT2+"')"
      

  3.   

    方便点的在数据库中设置允许为空的地方打钩
    C# 中string strText=Text.text.tostring();
      

  4.   

    string strText;
    if TEXT1.text = ""
    strText = "NULL"
    else
    strText = TEXT1.textsql="insert into test values('"+strText+"')"
      

  5.   

    如果为空则插入System.DBNull.Value
      

  6.   

    System.DBNull.Value if (txtID.Text.Trim() ="" )
                {
                    SqlParameter pa = new SqlParameter("id").Value = System.DBNull.Value;
                }