我正在关注this教程,以便在用户输入时从SQL服务器创建动态搜索结果。它告诉我创建一个.asmx文件,这不是我之前曾经使用过的格式。这是我到目前为止的代码:
WebService.asmx.cs:
public class SearchService : WebService
{
[WebMethod]
public searchResult[] Search(string txtSearch)
{
//Declare collection of searchResult
List resultList = new List();
var db = Database.Open("mPlan");
var result = db.Query("SELECT * from Users where Username like '%" + txtSearch + "%'");
try
{
foreach(var record in result)
{
searchResult result = new searchResult();
result.Username = ["Username"].ToString();
resultList.Add(result);
}
return resultList.ToArray();
}
catch
{
return null;
}
}}
WebService.asmx:
<%@ WebService Language="C#" class="WebService.asmx.cs" %>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.GenerateScriptType(typeof(searchResult))]
public class searchResult
{
public string Title;
public string img;
public string href;
}
这是我的错误消息,有人可以帮我这个吗?
编译器错误消息: BC30689:无法显示语句 在方法体之外
答案 0 :(得分:0)
您的Web方法必须在类中声明,并且属性必须装饰该类。
[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.GenerateScriptType(typeof(searchResult))]
public class SearchService : WebService
{
[WebMethod]
public searchResult[] Search(string txtSearch)
{
// ...
}
}