将 NULL SQL 参数值作为绑定到 Gridview 的 SelectParameters 传递
以下 SQL 查询在 SQL Management Studio 中完美运行:
DECLARE @phone AS varchar(25)
SET @phone = NULL
DECLARE @firstname AS varchar(25)
SET @firstname = NULL
DECLARE @lastname AS varchar(25)
SET @lastname = NULL
DECLARE @email AS varchar(300)
SET @email = NULL
SELECT id, lead_code, first_name + ' ' + last_name AS [name], lead_status, lead_source, date_entered, city, state, zip, gender, home_phone, call_back
FROM leads
WHERE (home_phone = @phone OR @phone IS NULL) AND
(first_name = @firstname OR @firstname IS NULL) AND
(last_name = @lastname OR @lastname IS NULL) AND
(email = @email OR @email IS NULL)
ORDER BY date_entered DESC
这是存储过程的顶部:
ALTER PROCEDURE dbo.custom_LeadsGetAllLeadsSimpleSearchParams
(
@phone varchar(25) = null,
@firstname varchar(25) = null,
@lastname varchar(25) = null,
@email varchar(300) = null
)
AS
SELECT id.....
以这种方式运行它会为我提供数据库中的所有 2100 行,当我更改其中一个 SET 命令来搜索姓名或电子邮件地址时,它仅返回这些行,我认为我的 SQL 正在工作(虽然可能不是很好)。
问题出在.NET 部分。
从我正在执行的页面的代码隐藏中:
sd.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
sd.SelectCommand = "custom_LeadsGetAllLeadsSimpleSearchParams";
sd.SelectParameters.Add("@phone", strPhone);
sd.SelectParameters.Add("@firstname", strFirstName);
sd.SelectParameters.Add("@lastname", strLastName);
sd.SelectParameters.Add("@email", strEmail);
gvLeads.DataBind();
当 null 时,字符串变量作为 null 传递。当我在调试器中查看 sd.SelectParameters 集合时,我会在需要的时间和位置看到参数值为 null。问题是当数据绑定发生时,网格渲染时没有数据。我刚刚得到我设置的 gridview no rows 文本。有想法吗?
The following SQL query WORKS perfectly in SQL Management Studio:
DECLARE @phone AS varchar(25)
SET @phone = NULL
DECLARE @firstname AS varchar(25)
SET @firstname = NULL
DECLARE @lastname AS varchar(25)
SET @lastname = NULL
DECLARE @email AS varchar(300)
SET @email = NULL
SELECT id, lead_code, first_name + ' ' + last_name AS [name], lead_status, lead_source, date_entered, city, state, zip, gender, home_phone, call_back
FROM leads
WHERE (home_phone = @phone OR @phone IS NULL) AND
(first_name = @firstname OR @firstname IS NULL) AND
(last_name = @lastname OR @lastname IS NULL) AND
(email = @email OR @email IS NULL)
ORDER BY date_entered DESC
Here's the top of the sproc:
ALTER PROCEDURE dbo.custom_LeadsGetAllLeadsSimpleSearchParams
(
@phone varchar(25) = null,
@firstname varchar(25) = null,
@lastname varchar(25) = null,
@email varchar(300) = null
)
AS
SELECT id.....
Running it this way gives me all 2100 rows in my DB, when I change one of the SET commands to search for a name, or email address, it returns JUST those rows, I think my SQL is (while maybe not awesome) working.
The problem is in the .NET piece.
From a codebehind of the the page I'm doing this:
sd.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
sd.SelectCommand = "custom_LeadsGetAllLeadsSimpleSearchParams";
sd.SelectParameters.Add("@phone", strPhone);
sd.SelectParameters.Add("@firstname", strFirstName);
sd.SelectParameters.Add("@lastname", strLastName);
sd.SelectParameters.Add("@email", strEmail);
gvLeads.DataBind();
the string variables are passed as null when null. When I watch the sd.SelectParameters collection in the debugger, I'm seeing the parameter value as null when and where needed. The problem is that when the databind occurs, the grid renders with no data. I just get the gridview no rows text that I have set. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过添加此参数?
CancelSelectOnNullParameter
像这样:
Have you tried adding this parameter?
CancelSelectOnNullParameter
Like this:
sd.CancelSelectOnNullParameter = false;
sd.CancelSelectOnNullParameter = false;
您是否尝试过将其添加到每个参数中:
我不知道 ConvertEmptyStringToNull 是否将参数值转换为 NULL 或 DbNull.Value,但您可以尝试使用 DbNull.Value 而不是 NULL。
编辑:再想一想,如何仅在参数可用时才包含参数:
我认为这个解决方案会起作用,因为当它为空时,您只是将其完全排除。
Have you tried adding this to each argument:
I don't know if ConvertEmptyStringToNull converts the parameter value to NULL or DbNull.Value, but you might try using DbNull.Value instead of NULL.
EDIT: On second thought, how about including the parameters only when they're available:
I think this solution will work, because you're just excluding it altogether when it's null.