2012-08-03 157 views
0

我目前有一個jQuery自動完成插件的查詢字符串,但應該使用存儲過程。任何人都可以幫助我轉換?我這樣做似乎沒有工作。轉換爲存儲過程

原始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 + '%' 

謝謝!

回答

1

您需要將SqlCommand'CommandType'設置爲'CommandType.StoredProcedure'。

cmd.CommandType = CommandType.StoredProcedure; 

我還建議使用「sp_」以外的前綴。這就是微軟用於他們的系統程序,你可能會意外覆蓋你想要保留的一個。 :)

我這是怎麼生成的參數:

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)); 
+0

很高興知道SP_我是如何受教的。我只是在我調用存儲過程之前添加您提供的代碼?另外,它似乎並不喜歡cmd.executeReader()行 – user1512593 2012-08-03 23:01:43

+1

是的,把它放在你指定'CommandText'的位置,如果你給我錯誤從cmd.ExecuteReader我可以幫忙。它看起來像你兩次添加你的'@SearchText'參數。 – jtimperley 2012-08-03 23:10:50

+0

你是對的! 「 」過程或函數SP_AUTOCOMPLETE指定的參數太多。「 – user1512593 2012-08-03 23:12:12