检查值是否存在或为空

时间:2014-06-16 06:37:44

标签: c#

一个问题......我在下面的示例中有一个代码...我必须在数据库中插入一些值...但是直接从数据行中插入..我不想使用变量。所以我的问题是,有没有办法检查数据行中的值是否存在或者是否为null,如果该值不存在,我必须插入null或者如果它为null,则只插入null ...

示例:

myQuery = " INSERT INTO AGR3PL_CS (IDOC_NUM, PSEG_NUM, SEG_NUM, COMP01, GLNO01, NAME01) " +
          " VALUES (" + Lidoc_num + ", '" +
                        PSEG_NUM + "','" +
                        SEG_NUM + "','" +
                        dr_art_custsuply["Id"] + "','" +
                        dr_art_custsuply["GLN"] + "','" +
                        dr_art_custsuply["Name"] + "')";

这是我不想使用的方式......

if (!dr_art_custsuply.Table.Columns.Contains("GLN") || dr_art_custsuply["GLN"].ToString().Length <= 0)
{
    gln = "";
}
else
{
    gln = dr_art_custsuply["GLN"].ToString();
}

5 个答案:

答案 0 :(得分:7)

  1. 参数
  2. DBNull.Value
  3. 例如(因为你使用的是oracle):

    cmd.CommandText = @"
    INSERT INTO AGR3PL_CS (IDOC_NUM, PSEG_NUM, SEG_NUM, COMP01, GLNO01, NAME01)
    VALUES (:IDOC_NUM, :PSEG_NUM, :SEG_NUM, :COMP01, :GLNO01, :NAME01)";
    cmd.Parameters.AddWithValue("IDOC_NUM", Lidoc_num);
    cmd.Parameters.AddWithValue("PSEG_NUM", PSEG_NUM);
    cmd.Parameters.AddWithValue("SEG_NUM", SEG_NUM);
    cmd.Parameters.AddWithValue("COMP01", dr_art_custsuply["Id"] ?? DBNull.Value);
    cmd.Parameters.AddWithValue("GLNO01", dr_art_custsuply["GLN"] ?? DBNull.Value);
    cmd.Parameters.AddWithValue("NAME01", dr_art_custsuply["Name"] ?? DBNull.Value);
    cmd.ExecuteNonQuery();
    

    或者如果您想通过“dapper”获得一些帮助(因为您提到参数的数量是个问题):

    connection.Execute(@"
    INSERT INTO AGR3PL_CS (IDOC_NUM, PSEG_NUM, SEG_NUM, COMP01, GLNO01, NAME01)
    VALUES (:Lidoc_num, :PSEG_NUM, :SEG_NUM, :Comp, :Gln, :Name)",
        new { Lidoc_num, PSEG_NUM, SEG_NUM,
            Comp = (int?)dr_art_custsuply["Id"],
            Gln = (string)dr_art_custsuply["GLN"],
            Name = (string)dr_art_custsuply["Name"] });
    

答案 1 :(得分:3)

简单地做

Convert.ToString(dr_art_custsuply["GLN"])

如果有值,则发送该值。如果Convert.ToString()处理null,则为null。

注意:不要.ToString(),因为当值为null时它会给出异常。
阅读本Difference between Convert.ToString() and .ToString()

答案 2 :(得分:2)

永远不要将值插入SQL,而是使用 parameters / Bind 变量:

using Oracle.DataAccess.Client; // <- In case of using Oracle 

...

String myQuery = 
 @"insert into AGR3PL_CS(
     IDOC_NUM, 
     PSEG_NUM, 
     SEG_NUM, 
     COMP01, 
     GLNO01, 
     NAME01)
   values (
     :PRM_IDOC_NUM, -- <- Assuming that you use Oracle; @PRM_IDOC_NUM for MS SQL
     :PRM_PSEG_NUM, 
     :PRM_SEG_NUM, 
     :PRM_COMP01, 
     :PRM_GLNO01, 
     "PRM_NAME01)"; 

  ....

  using(OracleCommand cmd = new OracleCommand()) {
    ...
    cmd.CommandText = myQuery;

    // For MS SQL use
    // cmd.Parameters.AddWithValue("@PRM_IDOC_NUM", Lidoc_num); 
    cmd.Parameters.Add(":PRM_IDOC_NUM", Lidoc_num);
    cmd.Parameters.Add(":PRM_PSEG_NUM", PSEG_NUM);
    cmd.Parameters.Add(":PRM_SEG_NUM", SEG_NUM);
    cmd.Parameters.Add(":PRM_COMP01", dr_art_custsuply["Id"]);
    cmd.Parameters.Add(":PRM_GLNO01", dr_art_custsuply["GLN"]);
    cmd.Parameters.Add(":PRM_NAME01", dr_art_custsuply["Name"]);

    cmd.ExecuteNonQuery();
  }

答案 3 :(得分:1)

使用内联三元运算符:

 gln = (!dr_art_custsuply.Table.Columns.Contains("GLN") || dr_art_custsuply["GLN"].ToString().Length <= 0 ?  string.Empty : dr_art_custsuply["GLN"].ToString());

所以myQuery变为:

myQuery = " INSERT INTO AGR3PL_CS (IDOC_NUM, PSEG_NUM, SEG_NUM, COMP01, GLNO01, NAME01) " +
          " VALUES (" + Lidoc_num + ", '" +
                        PSEG_NUM + "','" +
                        SEG_NUM + "','" +
                        dr_art_custsuply["Id"] + "','" +
                        (!dr_art_custsuply.Table.Columns.Contains("GLN") || dr_art_custsuply["GLN"].ToString().Length <= 0 ?  string.Empty : dr_art_custsuply["GLN"].ToString()) + "','" +
                        dr_art_custsuply["Name"] + "')";

编辑:回答评论中的问题:

myQuery = " INSERT INTO AGR3PL_CS (IDOC_NUM, PSEG_NUM, SEG_NUM, COMP01, GLNO01, NAME01) " +
          " VALUES (" + Lidoc_num + ", '" +
                        PSEG_NUM + "','" +
                        SEG_NUM + "','" +
                        dr_art_custsuply["Id"] + "','" +
                        (!dr_art_custsuply.Table.Columns.Contains("GLN") || dr_art_custsuply["GLN"].ToString().Length <= 0 ? 0 : dr_art_custsuply["GLN"].ToString()) + "','" +
                        (!dr_art_custsuply.Table.Columns.Contains("Name") || dr_art_custsuply["Name"].ToString().Length <= 0 ? String.Empty : dr_art_custsuply["Name"].ToString()) + "')";

答案 4 :(得分:0)

您可以执行类似的操作(并将其用于预期的大多数类型):

var customerId = (CheckNull(dr_art_custsuply["Id"], String.Empty)).ToString();


public static object CheckNull(object value, object defaultValue)
{
     return ((value == null || value == DBNull.Value) ? defaultValue : value);
}