简体   繁体   English

dbCommand.Append 语法错误,使用@parameter 将 EQUALS 转换为 LIKE

[英]dbCommand.Append syntax error converting EQUALS to LIKE with @parameter

I have a .NET C# webform.我有一个 .NET C# 网络表单。 The CodeBehind picks up some textbox entry to append to a select statement for the txtboxes that are used. CodeBehind 将一些文本框条目提取到 append 到 select 语句用于使用的 txtboxes。 Here are the ASP textboxes:以下是 ASP 文本框:

<asp:TextBox ID="txtBadge" runat="server" CssClass="txtBadgeStyle"></asp:TextBox>    
<asp:TextBox ID="txtFirst" runat="server" CssClass="txtFirstStyle"></asp:TextBox>
<asp:TextBox ID="txtLast" runat="server" CssClass="txtLastStyle"></asp:TextBox>

Here's the (current wrong version) server side:这是(当前错误的版本)服务器端:

StringBuilder sbCommand = new StringBuilder("SELECT a.BADGE_ID, a.FIRSTNAME, a.LASTNAME, a.TITLE, b.deptname, CASE WHEN a.ACTIVE=1 THEN 'Yes' ELSE 'No' END FROM XA_EMPMAS a left join dept_master b on b.deptnum = a.deptnum where 1 = 1");

if (string.IsNullOrEmpty(txtBadge.Text) == false)
{
    sbCommand.Append(" AND a.BADGE_ID=@txtBadge");
    SqlParameter param = new SqlParameter("@txtBadge", txtBadge.Text);
    cmd.Parameters.Add(param);
}

if (string.IsNullOrEmpty(txtFirst.Text) == false)
{
    sbCommand.Append(" AND a.FIRSTNAME=@txtFirst");
    SqlParameter param = new SqlParameter("@txtFirst", txtFirst.Text);
    cmd.Parameters.Add(param);
}

if (string.IsNullOrEmpty(txtLast.Text) == false)
{
    sbCommand.Append(" AND a.LASTNAME like " + @txtLast + "%");
    SqlParameter param = new SqlParameter("@txtLast", txtLast.Text);
    cmd.Parameters.Add(param);
}

So I want to convert the first name and last name appends to LIKE instead of EQUALS.所以我想将名字和姓氏转换为 LIKE 而不是 EQUALS。 Leaving FIRSTNAME alone for now, I've been fooling around with LASTNAME and haven't had any success.暂时不要管 FIRSTNAME,我一直在玩 LASTNAME,但没有取得任何成功。

Can someone give me the syntax correction for using like in the append command please?有人可以给我在 append 命令中使用的语法更正吗?

sbCommand.Append(" AND a.LASTNAME like " + @txtLast + "%");

Thanks谢谢

This is not doing what you expect:这不是你所期望的:

sbCommand.Append(" AND a.LASTNAME like " + @txtLast + "%");

The string concatenation is off, leaving @txtLast as an identifier in C#, rather than part of the SQL.字符串连接已关闭,将@txtLast作为标识符保留在 C# 中,而不是 SQL 的一部分。 The @ character is C# is used to prefix identifiers if they might match a keyword. @字符是 C# 用于在标识符可能匹配关键字时作为标识符的前缀。 You have a textbox named txtLast , making this code legal with an implicit conversion to string.您有一个名为txtLast的文本框,通过隐式转换为字符串使此代码合法。

As a result, if it even compiles the resulting LIKE statement is probably not legal in SQL.因此,如果它甚至编译生成的LIKE语句,在 SQL 中可能是不合法的。 You're probably ending up with a condition in the SQL like this:您可能会在 SQL 中遇到这样的情况:

AND a.LaSTNAME like System.Web.UI.WebControls.Textbox%

Aside from the type name instead of the text value, this is also missing the required single quotes.除了类型名称而不是文本值之外,这还缺少必需的单引号。

You probably wanted this:你可能想要这个:

sbCommand.Append(" AND a.LASTNAME like @txtLast + '%'");

Or this:或这个:

sbCommand.Append(" AND a.LASTNAME like @txtLast");
SqlParameter param = new SqlParameter("@txtLast", txtLast.Text + "%");

Additionally, you should consider supplying all of the type information for your parameters.此外,您应该考虑为您的参数提供所有类型信息。 If you don't, ADO.Net is left to guess at the types and lengths.如果您不这样做,ADO.Net 就只能猜测类型和长度。 It tends to assume NVARCHAR and 50 (or the exact length of the text).它倾向于假设 NVARCHAR 和 50(或文本的确切长度)。 Either of those assumptions can cause big performance problems when they are wrong, in terms of forcing index misses (which can be HUGE) or per-row conversions for every row in the table (which can also be huge).这些假设中的任何一个在错误时都可能导致严重的性能问题,例如强制索引未命中(可能是巨大的)或表中每一行的每行转换(也可能是巨大的)。

if (!string.IsNullOrEmpty(txtLast.Text))
{
    sbCommand.Append(" AND a.LASTNAME like @txtLast + '%'");
    SqlParameter param = new SqlParameter("@txtLast", SqlDbtype.NVarChar, 50);
    param.Value = txtLast.Text;
    cmd.Parameters.Add(param);
}

Or, shorter:或者,更短:

if (!string.IsNullOrEmpty(txtLast.Text))
{
    sbCommand.Append(" AND a.LASTNAME like @txtLast + '%'");
    cmd.Parameters.Add("@txtLast", SqlDbType.NVarChar, 50).Value = txtLast.Text;
}

Especially for numbers and dates this can be important.特别是对于数字和日期,这可能很重要。 I would write the Badge_ID section like this:我会这样写Badge_ID部分:

if (!string.IsNullOrEmpty(txtBadge.Text))
{
    sbCommand.Append(" AND a.BADGE_ID=@txtBadge");
    cmd.Parameters.Add("@txtBadge", SqlDbtype.Int).Value = int.Parse(txtBadge.Text);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM