小编典典

AddWithValue参数为NULL时发生异常

c#

我有以下代码用于指定SQL查询的参数。我在使用时遇到了异常Code 1;但是当我使用时工作正常Code 2。在这里,Code 2我们检查是否为null,因此是否为if..else块。

例外:

参数化查询’{@application_ex_id nvarchar(4000))SELECT E.application_ex_id
A’期望参数’@application_ex_id’未提供。

代码1

command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);

代码2

if (logSearch.LogID != null)
{
         command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
}
else
{
        command.Parameters.AddWithValue("@application_ex_id", DBNull.Value );
}

  1. 您能否解释一下为什么它无法从代码1中的logSearch.LogID值中获取NULL(但能够接受DBNull)?

  2. 有更好的代码来处理吗?

参考

  1. 将空值分配给SqlParameter
  2. 返回的数据类型因表中的数据而异
  3. 从数据库smallint到C#可为空的int的转换错误
  4. DBNull的意义是什么?

    public Collection<Log> GetLogs(LogSearch logSearch)
    {
        Collection<Log> logs = new Collection<Log>();

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            string commandText = @"SELECT  *
                FROM Application_Ex E 
                WHERE  (E.application_ex_id = @application_ex_id OR @application_ex_id IS NULL)";

            using (SqlCommand command = new SqlCommand(commandText, connection))
            {
                command.CommandType = System.Data.CommandType.Text;

                //Parameter value setting
                //command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
                if (logSearch.LogID != null)
                {
                    command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
                }
                else
                {
                    command.Parameters.AddWithValue("@application_ex_id", DBNull.Value );
                }

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        Collection<Object> entityList = new Collection<Object>();
                        entityList.Add(new Log());

                        ArrayList records = EntityDataMappingHelper.SelectRecords(entityList, reader);

                        for (int i = 0; i < records.Count; i++)
                        {
                            Log log = new Log();
                            Dictionary<string, object> currentRecord = (Dictionary<string, object>)records[i];
                            EntityDataMappingHelper.FillEntityFromRecord(log, currentRecord);
                            logs.Add(log);
                        }
                    }

                    //reader.Close();
                }
            }
        }

        return logs;
    }

阅读 564

收藏
2020-05-19

共1个答案

小编典典

烦人的,不是吗。

您可以使用:

command.Parameters.AddWithValue("@application_ex_id",
       ((object)logSearch.LogID) ?? DBNull.Value);

或者,使用“ dapper”之类的工具,它将为您解决所有麻烦。

例如:

var data = conn.Query<SomeType>(commandText,
      new { application_ex_id = logSearch.LogID }).ToList();

很想 在dapper中添加一个方法来获取IDataReader…尚不确定是否是个好主意。

2020-05-19