下面是为SQL查询指定参数的代码.当我使用Code 1时,我得到以下异常;但当我使用Code 2时效果很好.在Code 2中,我们判断null,因此有if..else块.

例外情况:

The parameterized query '(@application_ex_id nvarchar(4000))SELECT E.application_ex_id A' expects the parameter '@application_ex_id', which was not supplied.

Code 1:

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

Code 2:

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

QUESTION

  1. 你能解释一下为什么不能从logSearch中获取NULL吗.代码1中的LogID值(但能够接受DBNull)?

  2. 有更好的代码来处理这个问题吗?

Reference:

  1. Assign null to a SqlParameter
  2. Datatype returned varies based on data in table
  3. Conversion error from database smallint into C# nullable int
  4. What is the point of 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;
    }

推荐答案

真烦人,不是吗.

您可以使用:

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

或者,使用像"衣冠楚楚"这样的工具,它会为你做所有的杂事.

例如:

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

我是tempted,要给dapper添加一个方法来获得IDataReader...目前还不确定这是否是个好主意.

.net相关问答推荐

.NET restore/build在使用组织包的Github Action工作流中调用时获得401

在PowerShell中,XML子对象和属性是对象属性.它怎麽工作?

Azure Function应用程序-如何升级.NET运行时

在 .NET 中使用 AES 解密时缺少后半字节

如何确定计时器是否正在运行?

SetupSet() 已过时.代替什么?

IIS Express - 500.19 无法读取配置文件 - 因为它正在查看错误的路径

ILMerge 最佳实践

.NET 4.0 中有内置的二叉搜索树吗?

如何在 WPF 应用程序中使用 App.config 文件?

调用委托与方法的性能

在 .NET 中计算目录大小的最佳方法是什么?

在生产环境中部署调试符号(pdb 文件)有什么风险?

Microsoft.Bcl.Build NuGet 包有什么作用?

了解 C# 中的协变和逆变接口

.NET 委托类型的正确命名约定?

System.Array.CopyTo() 和 System.Array.Clone() 之间的区别

当它被抛出和捕获时,不要在那个异常处停止调试器

在 try/catch/finally 中等待的一个很好的解决方案?

从不同程序集中的类名中解析类型