下面是为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 Blazor-使用子组件中的处理程序方法进行双向数据绑定

如何在dotnet中使用OpenTelemetry Prometheus导出器导出多个版本的度量?

仅使用 .NET GetBytes 方法转换有效字节而不创建问号

.net Maui 在单击按钮时访问 CollectionView 中的数据项

双精度的 C++ 和 C# 十六进制值之间的差异

关于在 .NET 中干净地终止线程的问题

我应该创建一个 DateRange 对象吗?

在c#中计算中位数

AutoMapper 的替代品

使用 IIS Express 托管网站(临时)

[DllImport("QCall")] 是什么?

是否有 TLS 1.2 的 .NET 实现?

将 C# 编译为本机?

将接收到的对象转换为 List 或 IEnumerable

ILookup 接口与 IDictionary

等效于 C# 在 powershell 中的使用关键字?

有没有像样的 C# 分析器?

如何在 C# 中以编程方式安装 Windows 服务?

如何从 .NET 读取 PEM RSA 私钥

System.ServiceModel 在 .NET Core 项目中找不到