I currently have a query string for the jQuery Autocomplete plug in but should be using a stored procedure instead. Can anyone help me convert? It seems to not be working when I do it.
Original 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());
}
}
}
New ASHX for stored procedure:
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());
}
}
}
Stored procedure:
@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 + '%'
Thanks!
You need to set the SqlCommand 'CommandType' to 'CommandType.StoredProcedure'.
cmd.CommandType = CommandType.StoredProcedure;
I would also recommend using a prefix other than 'sp_'. That is what Microsoft used for their system procedures and you might accidentally overwrite one you want to keep around. :)
This is how I generate parameters:
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;
}
And I just do this.
cmd.Parameters.Add(GetParameter("@SearchText", searchText, SqlDbType.VarChar));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.