gpt4 book ai didi

c# - 存储过程根据过滤器查看所有记录不接受空值

转载 作者:行者123 更新时间:2023-11-30 20:30:37 24 4
gpt4 key购买 nike

我有一个存储客户记录的表,我想过滤这些客户,但我无法将空值放入存储过程。这些值有时可以为 null,我在 sql 中对其进行了测试,它可以工作,但它不会接受来自后面的 c# 的 null 值。我尝试了 if 语句,结果是一样的。

感谢任何帮助

您可以查看下面的代码

SQL

WHERE ( ProcessStatus.ProcessStatusKey = @ProcessStatusKey OR @ProcessStatusKey IS NULL )
AND
( PriorityLevels.PriorityLevelKey = @PriorityLevelKey OR @PriorityLevelKey IS NULL )
AND
( SystemUsers.SystemUserKey = @SystemUserKey OR @SystemUserKey IS NULL )
AND
( CaseHoganData.LoanAccountNumber = @AccountNumber OR @AccountNumber IS NULL )
AND
( ( LTRIM(RTRIM(( ISNULL(Customers.FirstName, '') + ' ' + ISNULL(Customers.LastName, '') ))) ) LIKE '%'
+ @CustomerName + '%'OR @CustomerName IS NULL )

ASP.NET

    using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("sp_Cases_ViewAll", con))
{
//if (prioritylev == "")
// cmd.Parameters.AddWithValue("@PriorityLevelKey", DBNull.Value);
//else


cmd.Parameters.AddWithValue("@PriorityLevelKey", (string.IsNullOrEmpty(prioritylev) ? (Guid?)null : new Guid(prioritylev)));

cmd.Parameters.AddWithValue("@ProcessStatusKey", (string.IsNullOrEmpty(processkey) ? (Guid?)null : new Guid(processkey)));
cmd.Parameters.AddWithValue("@SystemUserKey", (string.IsNullOrEmpty(systemuser) ? (Guid?)null : new Guid(systemuser)));
cmd.Parameters.AddWithValue("@AccountNumber", accnumber);
cmd.Parameters.AddWithValue("@CustomerName", cusname);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
DataTable dt = new DataTable();
sda.Fill(dt);
grid_Cases.DataSource = dt;
grid_Cases.DataBind();

}
}
}

编辑这对我有用

cmd.Parameters.AddWithValue("@PriorityLevelKey", (string.IsNullOrEmpty(prioritylev) ? (object)DBNull.Value : new Guid(prioritylev)));

最佳答案

您在评论代码“DBNull.value”上走在正确的道路上,这是您更改后的代码:

            using (SqlCommand cmd = new SqlCommand("sp_Cases_ViewAll", con))
{
//if (prioritylev == "")
// cmd.Parameters.AddWithValue("@PriorityLevelKey", DBNull.Value);
//else


if (string.IsNullOrEmpty(prioritylev))
{
cmd.Parameters.AddWithValue("@PriorityLevelKey", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@PriorityLevelKey", new Guid(prioritylev));
}


if(string.IsNullOrEmpty(processkey))
{
cmd.Parameters.AddWithValue("@ProcessStatusKey", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@ProcessStatusKey", new Guid(processkey));
}


if (string.IsNullOrEmpty(systemuser))
{
cmd.Parameters.AddWithValue("@SystemUserKey", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@SystemUserKey", new Guid(systemuser));
}


cmd.Parameters.AddWithValue("@AccountNumber", accnumber);

cmd.Parameters.AddWithValue("@CustomerName", cusname);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
DataTable dt = new DataTable();
sda.Fill(dt);
grid_Cases.DataSource = dt;
grid_Cases.DataBind();

}
}
}

关于c# - 存储过程根据过滤器查看所有记录不接受空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44523037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com