【问题标题】:Insert empty textbox as NULL in database在数据库中将空文本框插入为 NULL
【发布时间】:2014-06-09 16:05:12
【问题描述】:

如果 texbox 为空,我想在数据库中插入 NULL。 我有 2 个文本框名称:txtBox1、txtBox2、txtBox3 和此代码:

SqlConnection con = new SqlConnection(@"Data Source=ALEX-PC\SQLEXPRESS;Initial Catalog=Asig;Integrated Security=True;");
SqlCommand cmd;

.................

con.Open();
cmd = new SqlCommand("INSERT INTO asigpag( Data1,Data2,Data3 ) VALUES ('" + txtBox1.Text + "','"  + txtBox2.Text + "','" + txtBox3.Text + "')",con);

cmd.ExecuteNonQuery();
MessageBox.Show("Succes ! ");

这会在数据库中正确插入所有三个文本框的数据。但如果其中一个文本框为空,则在数据库中它会显示为空字段而不是 NULL。

【问题讨论】:

    标签: c# mysql sql


    【解决方案1】:

    您可以检查TextBox.Text 是否有空字符串

    cmd = new SqlCommand("INSERT INTO asigpag ( Data1,Data2,Data3 ) VALUES (" + (!String.IsNullOrEmpty(txtBox1.Text) ? "'" + txtBox1.Text + "'" : "NULL") + "," + (!String.IsNullOrEmpty(txtBox2.Text) ? "'" + txtBox2.Text + "'" : "NULL") + "," + (!String.IsNullOrEmpty(txtBox3.Text) ? "'" + txtBox3.Text + "'" : "NULL") + ")",con);
    

    【讨论】:

    • 工作得很好。谢谢。
    【解决方案2】:

    试试这个:

    con.Open();
    cmd = new SqlCommand("INSERT INTO asigpag( Data1,Data2,Data3 ) VALUES (" + GetDbValue(txtBox1.Text) + ", "  + GetDbvalue(txtBox2.Text) + " , " + GetDbValue(txtBox3.Text) + ")",con);
    
    cmd.ExecuteNonQuery();
    MessageBox.Show("Succes ! ");
    
    
    private String GetDbValue(String data)
    {
        if (String.IsNullOrEmpty(data)
            return "NULL";
        else
            return "'" + data + "'";
    }
    

    您使用非常错误的方式将数据插入数据库,请考虑使用参数化查询。

    您的查询容易受到Sql Injection的攻击

    【讨论】:

      【解决方案3】:

      你可以像这样在那边添加条件:

      con.Open();
      cmd = new SqlCommand("INSERT INTO asigpag( Data1,Data2,Data3 ) VALUES ('" + txtBox1.Text==""?"null":txtBox1.Text +     "','"  + txtBox2.Text==""?"null":txtBox2.Text + "','" + txtBox3.Text==""?"null":txtBox3.Text + "')",con);
      
      cmd.ExecuteNonQuery();
      MessageBox.Show("Succes ! ");
      

      或检查一下

      http://www.codeproject.com/Questions/671726/Inserting-null-value-into-database-from-textbox-us

      【讨论】:

        【解决方案4】:

        这将检查文本框,如果框为空,则插入 DBNull。它也被参数化以避免注射。 还值得一看 using statements for your connection and command,这里有一个很好的例子http://www.dotnetperls.com/sqldatareader

        SqlConnection con = new SqlConnection(@"Data Source=ALEX-PC\SQLEXPRESS;Initial Catalog=Asig;Integrated Security=True;");
        SqlCommand cmd;
        con.Open();
        cmd = new SqlCommand("INSERT INTO asigpag( Data1,Data2,Data3 ) VALUES ('@textBox1','@textBox1','@textBox1')",con);
        cmd.Parameters.AddWithValue("@textBox1", string.IsNullOrEmpty(txtBox1.Text) ? (object)DBNull.Value : txtBox1.Text);
        cmd.Parameters.AddWithValue("@textBox2", string.IsNullOrEmpty(txtBox2.Text) ? (object)DBNull.Value : txtBox2.Text);
        cmd.Parameters.AddWithValue("@textBox3", string.IsNullOrEmpty(txtBox3.Text) ? (object)DBNull.Value : txtBox3.Text);
        cmd.ExecuteNonQuery();
        MessageBox.Show("Succes ! ");
        

        【讨论】:

          【解决方案5】:

          试试这个:

          SqlConnection con = new SqlConnection(@"Data Source=ALEX-PC\SQLEXPRESS;Initial Catalog=Asig;Integrated Security=True;");
          SqlCommand cmd;
          
          .................
          
          con.Open();
          cmd = new SqlCommand(
              "INSERT INTO asigpag( Data1,Data2,Data3 ) VALUES ('" + 
              string.IsNullOrEmpty(txtBox1.Text) ? null : txtBox1.Text +  "','"  +
              string.IsNullOrEmpty(txtBox2.Text) ? null : txtBox2.Text + "','" + 
              string.IsNullOrEmpty(txtBox3.Text) ? null : txtBox3.Text + "')",
              con);
          
          cmd.ExecuteNonQuery();
          MessageBox.Show("Success!");
          

          【讨论】:

          • 我还没有看到下面的帖子。当我打字时,这是张贴的。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-05
          • 2014-09-10
          • 2012-01-20
          • 1970-01-01
          • 2013-03-27
          相关资源
          最近更新 更多