将 NULL SQL 参数值作为绑定到 Gridview 的 SelectParameters 传递

发布于 2024-12-02 03:22:34 字数 1806 浏览 0 评论 0原文

以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕色琉璃 2024-12-09 03:22:34

您是否尝试过添加此参数?

CancelSelectOnNullParameter

像这样:

sd.CancelSelectOnNullParameter = false;

Have you tried adding this parameter?

CancelSelectOnNullParameter

Like this:

sd.CancelSelectOnNullParameter = false;
半寸时光 2024-12-09 03:22:34

sd.CancelSelectOnNullParameter = false;

sd.CancelSelectOnNullParameter = false;

深府石板幽径 2024-12-09 03:22:34

您是否尝试过将其添加到每个参数中:

sd.SelectParameters["@phone"].ConvertEmptyStringToNull = true;

我不知道 ConvertEmptyStringToNull 是否将参数值转换为 NULL 或 DbNull.Value,但您可以尝试使用 DbNull.Value 而不是 NULL。

编辑:再想一想,如何仅在参数可用时才包含参数:

if (!String.IsNullOrEmpty(strEmail))
    sd.SelectParameters.Add("@email", strEmail);

我认为这个解决方案会起作用,因为当它为空时,您只是将其完全排除。

Have you tried adding this to each argument:

sd.SelectParameters["@phone"].ConvertEmptyStringToNull = true;

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:

if (!String.IsNullOrEmpty(strEmail))
    sd.SelectParameters.Add("@email", strEmail);

I think this solution will work, because you're just excluding it altogether when it's null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文