0

我目前有一个 jQuery Autocomplete 插件的查询字符串,但应该使用存储过程。谁能帮我转换?当我这样做时,它似乎不起作用。

原装 ASHX

public class Search_CS : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    string prefixText = context.Request.QueryString["q"];
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["Rollup2ConnectionString"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            //cmd.CommandText = "select NUID from T_USER where " +
            //"NUID like @SearchText + '%'";
            cmd.CommandText = "select rtrim(NUID) NUID, rtrim(FNAME) FNAME, rtrim(LNAME) LNAME from T_USER where NUID like @SearchText + '%' OR FNAME like @SearchText + '%' OR LNAME like @SearchText + '%'"; 
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            StringBuilder sb = new StringBuilder(); 
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    sb.Append(sdr["NUID"].ToString() + " ").Append(sdr["FNAME"].ToString() + " ").Append(sdr["LNAME"].ToString() + " ")
                        .Append(Environment.NewLine); 
                }
            }
            conn.Close();
            context.Response.Write(sb.ToString()); 
        }
    }
}

用于存储过程的新 ASHX:

public class Search_CS : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    string prefixText = context.Request.QueryString["q"];
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["Rollup2ConnectionString"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            //cmd.CommandText = "select NUID from T_USER where " +
            //"NUID like @SearchText + '%'";
            cmd.CommandText = "SP_AUTOCOMPLETE"; 
            cmd.Parameters.AddWithValue("@SearchText", prefixText);

            cmd.Parameters.Add(new SqlParameter("@SearchText", SqlDbType.VarChar));
            cmd.Parameters["@SearchText"].Value = prefixText;


            cmd.Connection = conn;
            StringBuilder sb = new StringBuilder(); 
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    sb.Append(sdr["NUID"].ToString() + " ").Append(sdr["FNAME"].ToString() + " ").Append(sdr["LNAME"].ToString() + " ")
                        .Append(Environment.NewLine); 
                }
            }
            conn.Close();
            context.Response.Write(sb.ToString()); 
        }
    }
}

存储过程:

@SearchText VARCHAR(255)
AS
BEGIN

    SET NOCOUNT ON;
    SELECT RTRIM(NUID) NUID, RTRIM(FNAME) FNAME, RTRIM(LNAME) LNAME 
    FROM T_USER 
    WHERE NUID like @SearchText + '%' OR FNAME like @SearchText + '%' OR LNAME like @SearchText + '%'

谢谢!

4

1 回答 1

1

您需要将 SqlCommand 'CommandType' 设置为 'CommandType.StoredProcedure'。

cmd.CommandType = CommandType.StoredProcedure;

我还建议使用“sp_”以外的前缀。这就是 Microsoft 用于他们的系统过程的内容,您可能会不小心覆盖您想要保留的一个。:)

这就是我生成参数的方式:

public static SqlParameter GetParameter(string parameterName, object value, SqlDbType type, int size)
{
    if (value == null)
    {
        value = DBNull.Value;
    }

    if (size <= 0 && type == SqlDbType.VarChar)
    {
        switch (type)
        {
            case SqlDbType.VarChar:
                size = 8000;
                break;
            case SqlDbType.NVarChar:
                size = 4000;
                break;
        }
    }

    SqlParameter parameter = new SqlParameter(parameterName, type, size);
    parameter.Value = value;
    parameter.IsNullable = true;

    return parameter;
}

而我就是这样做的。

cmd.Parameters.Add(GetParameter("@SearchText", searchText, SqlDbType.VarChar));
于 2012-08-03T22:52:47.460 回答