我目前有一个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 + '%'
谢谢!
答案 0 :(得分: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));