gpt4 book ai didi

c# - 无法按照教程创建动态搜索结果

转载 作者:行者123 更新时间:2023-12-02 11:06:09 24 4
gpt4 key购买 nike

我正在遵循this教程,以在用户键入时从SQL Server创建动态搜索结果。它告诉我创建一个.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;
}

这是我的错误消息,有人可以帮助我吗?

Compiler Error Message: BC30689: Statement cannot appear outside of a method body

最佳答案

您的Web方法必须在一个类中声明,并且属性必须装饰该类。

[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.GenerateScriptType(typeof(searchResult))]
public class SearchService : WebService
{
[WebMethod]
public searchResult[] Search(string txtSearch)
{
// ...
}
}

关于c# - 无法按照教程创建动态搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9667899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com