我在 Visual Studio 2010 中创建了一个 Web 服务。我创建了一个包含命令及其描述的表的数据库。我的程序是:我的课程:-DataHelper.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for DataHelper
/// </summary>
public class DataHelper
{
public static string GetData(string Command)
{
string Explanation = " ";
SqlConnection con = new SqlConnection(@"Data Source=CSS-L3-D008;Initial Catalog=works1;Integrated Security=true;");
SqlCommand cmd = new SqlCommand("Select Explanation from Data1 where Command= '" + Command.ToUpper() + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Explanation = dr["Explanation"].ToString();
}
dr.Close();
con.Close();
return Explanation;
}
}
和Service1.asmx
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService
Private Property DataHelper As Object
<WebMethod()>
Public Function HelloWorld() As String
Return "Hello World"
End Function
<WebMethod()>
Public Function GetData(Command) As String
Return DataHelper.GetData(Command)
End Function
End Class
问题是,当我运行它时,我得到了 webmethods HelloWorld 和 GetData 但 GetData 不起作用。当我单击 HelloWorld() 时,我得到调用方法并且它运行正常。但是当我单击 GetData() 时,我得到调用按钮,然后它显示“测试表单仅适用于以原始类型作为参数的方法。” 实际上它应该给出一个框,我可以在其中输入命令,并且应该从 sql server 返回描述。请帮助我。