【问题标题】:Update the database maintaining the Null status更新保持 Null 状态的数据库
【发布时间】:2010-09-15 22:09:39
【问题描述】:

我有一个 asp.net 文本表单,其中包含许多可选的十进制字段。我想有选择地更新数据库,但不为没有数据的字段插入“0”(保持空状态)。

通常,我会创建多个函数,每个函数都有不同的签名来处理这个问题。但是,我通过不允许具有相同名称的函数具有多个签名的 Web 服务插入数据。我可以想出几种方法来解决这个问题,但没有一个是“实用的”。

【问题讨论】:

    标签: c# asp.net sql-server web-services


    【解决方案1】:

    您可以使用DBNull class 在您的网络服务代码中表示一个空值。

    虽然您仍然必须使用代理值(例如 0 或 -1),然后只需评估该值以将其转换为 DBNull 对象。

    【讨论】:

      【解决方案2】:

      Nullable Types 用于相同目的。它们表示可能没有数据的值类型。可以使用这些类型的 HasValue 属性检查值的存在。

      读取字段的伪代码:

      decimal? dValue; // default value is null
       if(decimalValueExists)
      {
        dValue = <value read from text file>
      } 
      

      当您说多个方法时 - 我假设这些是能够添加可选字段的重载方法(因此 n 个可选字段意味着 n 个更多方法)

      您可以通过编写单个方法来避免编写这些方法。假设您有一个必填字段和一个可选字段:

      public class MyFields
      {
          decimal  req1;
          decimal? opt1; // optional field 1  
      }
      

      然后定义使用它的web服务方法:

      [WebMethod]
      void MyWSMethod(MyFields myFields)
      {/* code here will ultimately call InsertMyFields */}
      
      void InsertMyFields(MyFields myFields)
      {
          using (SqlConnection connection = new SqlConnection(connectionString))
          {
              // Create the command and set its properties.
              SqlCommand command = new SqlCommand();
              command.Connection = connection;
              command.CommandText = "AddMyFields";
              command.CommandType = CommandType.StoredProcedure;
      
              // Add the required input parameter
              SqlParameter parameter1 = new SqlParameter();
              parameter1.ParameterName = "@ReqField1";
              parameter1.SqlDbType = SqlDbType.NVarChar;
              parameter1.Direction = ParameterDirection.Input;
              parameter1.Value = myFields.req1;
      
              // Add the parameter to the Parameters collection. 
              command.Parameters.Add(parameter1);
      
              // Add the optional parameter and set its properties.
              SqlParameter parameter2 = new SqlParameter();
              parameter2.ParameterName = "@OptField1";
              parameter2.SqlDbType = SqlDbType.NVarChar;
              parameter2.Direction = ParameterDirection.Input;
              parameter2.Value = myFields.opt1 ?? DBNull.Value; //null coalescing operator
      
              // Add the parameter to the Parameters collection. 
              command.Parameters.Add(parameter2);
      
              //.. rest of the code
          }
      
      } 
      

      如果可空类型有值,Null Coalescing Operator 将设置该值,否则它将设置您指定的其他值(在我们的例子中为 DBNull.Value)。

      【讨论】:

        【解决方案3】:

        您可以将参数定义为可为空的小数。可空值类型的 C# 语法如下:

        decimal? rebateAmountOrWhatever;
        

        然后您可以在变量中存储空值并将变量与空值进行比较。

        new SqlParameter("@RebateAmount", 
          rebateAmountOrWhatever == null ? (object)DBNull.Value : (object)rebateAmountOrWhatever)
        

        使用 ?? 也很有趣像这样的运算符:

        new SqlParameter("@RebateAmount", 
         (object)rebateAmountOrWhatever ?? (object)DBNull.Value)
        

        声明变量的等效方法是使用 Nullable 泛型类型,如下所示:

        Nullable<decimal> currentIraBalance = null;
        

        【讨论】:

          猜你喜欢
          • 2021-07-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-28
          • 2018-07-11
          相关资源
          最近更新 更多