【问题标题】:Insert statement won't insert Null value插入语句不会插入 Null 值
【发布时间】:2011-11-07 02:07:24
【问题描述】:

我正在尝试从 C# 向我的数据库中插入一个空值,如下所示:

SqlCommand command = new SqlCommand("INSERT INTO Employee
VALUES ('" + employeeID.Text + "','" + name.Text + "','" + age.Text
        + "','" + phone.Text + "','" + DBNull.Value + "')", connection);

DBNull.Value 是日期的位置,但我希望它等于 null,但它似乎输入了一个默认日期,1900 什么...

【问题讨论】:

  • 永远不要生成这样的 SQL 查询,而是使用命令参数。否则有人会输入一个名字为 "); DELETE * FROM EMPLOYEE; -- "
  • 这似乎允许SQLi。您知道其中的含义吗?

标签: c# asp.net sql


【解决方案1】:

改为:

SqlCommand command = new SqlCommand("INSERT INTO Employee VALUES ('" + employeeID.Text + "','" + name.Text + "','" + age.Text + "','" + phone.Text + "',null)", connection);

DBNull.Value.ToString() 返回空字符串,但您希望改为 null。

但是,这种构建查询的方式可能会导致问题。例如,如果您的字符串之一包含引号 ',则结果查询将引发错误。更好的方法是使用参数并在 SqlCommand 对象上设置:

SqlCommand command = new SqlCommand("INSERT INTO Employee VALUES (@empId,@name,@age,@phone,null)", connection);
command.Parameters.Add(new SqlParameter("@empId", employeeId.Text));
command.Parameters.Add(new SqlParameter("@name", name.Text));
command.Parameters.Add(new SqlParameter("@age", age.Text));
command.Parameters.Add(new SqlParameter("@phone", phone.Text));

【讨论】:

    【解决方案2】:

    使用参数。

    SqlCommand command = new SqlCommand("INSERT INTO Employee VALUES 
                (@employeeID,@name,@age,@phone,@bdate)",connection);
    ....
    command.Parameters.AddWithValue("@bdate",DBNull.Value);
    //or
    command.Parameters.Add("@bdate",System.Data.SqlDbType.DateTime).Value=DBNull.Value;
    

    或者试试这个,

     SqlCommand command = new SqlCommand("INSERT INTO Employee 
             (employeeID,name,age,phone) VALUES 
                    (@employeeID,@name,@age,@phone)",connection);
    

    【讨论】:

      【解决方案3】:

      将 DBNull.Value 更改为动态 SQL 的文字 null:

      SqlCommand command = new SqlCommand("INSERT INTO Employee VALUES ('" + employeeID.Text + "','" + name.Text + "','" + age.Text + "','" + phone.Text + "',null)", connection);
      

      【讨论】:

        【解决方案4】:

        试试这个:

        SqlCommand command = new SqlCommand();
        command.ComandText = "insert into employee values(@employeeId, @name, @age, @phone, @someNullVal)";
        command.Parameters.AddWithValue("@employeedId", employeedID.Text);
        // all your other parameters
        command.Parameters.AddWithValue("@someNullVal", DBNull.Value);
        

        这解决了两个问题。您明确的问题(将 NULL 值插入表中)和SQL Injection 潜力。

        【讨论】:

          【解决方案5】:

          如果你输出 "'" + DBNull.Value + "'" ,你会发现它是 '' ,这意味着你在数据库中插入了一个空字符串而不是 null 。所以,你只需要写 null:

          SqlCommand command = new SqlCommand("INSERT INTO Employee
          VALUES ('" + employeeID.Text + "','" + name.Text + "','" + age.Text
                  + "','" + phone.Text + "', null)", connection);
          

          【讨论】:

            【解决方案6】:

            像这样试试。

            SqlCommand command = new SqlCommand("INSERT INTO Employee VALUES ('" + employeeID.Text + 
                     "','" + name.Text + "','" + age.Text + "','" + phone.Text + "','Null')", connection);
            

            【讨论】:

            • 看来你在单引号中有 Null :'Null' 我认为这会将字符串“Null”放入字段中。
            • 我的错,对不起。我会鼓励您将存储日期时间的数据库中的数据类型更改为 varchar,以便您可以拥有空值。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-21
            • 2018-04-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多