以下 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
这是 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.....
以这种方式运行它会给我数据库中的所有 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 text。想法?