Web services 从C中的WebMethod返回单个字符串值#

Web services 从C中的WebMethod返回单个字符串值#,web-services,c#-4.0,webmethod,Web Services,C# 4.0,Webmethod,我只想返回messageID,但它不能正常工作。如何返回单个字符串值 [WebMethod] public string messageComeGetID(string from, string to) { try { sqlConn.Open(); } catch (Exception e) { return null; }

我只想返回messageID,但它不能正常工作。如何返回单个字符串值

    [WebMethod]
    public string messageComeGetID(string from, string to)
    {
        try
        {
            sqlConn.Open();
        }
        catch (Exception e)
        {
            return null;
        }
        // select messageID from tblMessage where [from] = '2' AND [to] = '4' gibi.
        SqlCommand cmd3 = new SqlCommand("select messageID from tblMessage where [from]=@from AND [to]=@to", sqlConn);

        cmd3.Parameters.AddWithValue("@from", from);
        cmd3.Parameters.AddWithValue("@to", to);

        cmd3.Connection = sqlConn;
        object value = cmd3.ExecuteScalar();
        string messageID = Convert.ToString(value);
        cmd3.ExecuteNonQuery();
        cmd3.Dispose();
        sqlConn.Close(); 

        return messageID;
    }

我可以这样实现 WebService1.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

namespace StackOverflow_Solve.Services
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetMessage()   // Home.CS 
        {
            //return "GOGO";
            Context.Response.Output.Write("Hello World");
            Context.Response.End();
            return string.Empty;
        } 
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Script.Serialization;
使用System.Web.Script.Services;
使用System.Web.Services;
命名空间StackOverflow\u Solve.Services
{
/// 
///WebService 1的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类WebService 1:System.Web.Services.WebService
{
[网络方法]
公共字符串HelloWorld()
{
返回“你好世界”;
}
[网络方法]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共字符串GetMessage()//Home.CS
{
//返回“GOGO”;
Context.Response.Output.Write(“helloworld”);
Context.Response.End();
返回字符串。空;
} 
}
}
全面实施是必要的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<head> //HTML and Ajax 
<title></title>

 <script>
     function GetMessage() {
         //Load jQuery($) to Use 
         $(function() {
             $.get("WebService1.asmx/GetMessage", function (data) {
                 console.log(data);
                 $("p").html(data);
             });
         });

     }
 </script>
</head>
<body>
    <input type="button" onclick="GetMessage()" value="Get Message" />
    <p></p>
</body>
</body>
</html>

//HTML和Ajax
函数GetMessage(){
//加载jQuery($)以使用
$(函数(){
$.get(“WebService1.asmx/GetMessage”,函数(数据){
控制台日志(数据);
$(“p”).html(数据);
});
});
}


这正是返回单个字符串值的方式。为什么您认为它不起作用?我的sql表中有相同的from和to列,但id不同。我试图获取查询的最后一个id,但是上面的代码总是给我第一个id。谢谢。