0

VS 和 C# 的完整新手,我正在尝试设置一个 Web 服务,从非常简单的开始。我设法编写了一个类,它(希望)在给定项目代码的情况下返回项目的名称。现在我想实现它以便可以从网络上调用它,但我不知道如何检索参数。这个想法是拥有http://myurl.com?ProdRef=XYZ,并返回产品 XYZ 的名称。
请不要拍,这是我第一次尝试 C# 和 VS !

编辑:工作代码(对谁有帮助)

班级代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Class1
/// </summary>
class Product    
    {
        public string ProdRef {get; set; }
        public string ProdName{get; set; }

        public static string GetLabel(string ProdRef)
        {
            //
            // create a new SqlConnection object with the appropriate connection string 
            SqlConnection sqlConn = new SqlConnection("Data Source=ssss;Initial Catalog=ccccc;Persist Security Info=True;User ID=uuu;Password=pppp;Network Library=dbmssocn");
            // open the connection 
            sqlConn.Open();
            // create the command object 
            SqlCommand sqlComm = new SqlCommand("select libelle from dbo.vwArticlesPerm WHERE Ref = '" + ProdRef + "'", sqlConn);
            string strResult = (string)sqlComm.ExecuteScalar();
            // close the connection
            sqlConn.Close();
            return strResult;
        }
    }

网络服务代码:

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

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://xxxxx.lu/clientwebserv")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string GetName(string prName) {
        return Product.GetLabel(prName);
    }

}
4

1 回答 1

1

我认为您应该以这种方式定义 GetName

[WebMethod]
public string GetName(string name) 
{
    return Product.GetLLabel(name);
}

当客户端应用程序加载您的 WSDL 时,返回的 xml 显示该GetName函数需要输入字符串并返回字符串。就这样。

下一步:不要构建查询手动加入字符串、int、日期,使用参数。

SqlCommand sqlComm = new SqlCommand(
    "select libelle from dbo.vwArticlesPerm WHERE Ref = @par", sqlConn);
cmd.Parameters.AddWithValue("@par", ProdRef);

如果你想使用GetLabel,你必须添加public到它的声明中,否则你将无法在它的类之外看到它!

public static string GetLabel(string ProdRef)
{
    string strResult = null;
    using (SqlConnection sqlConn = new SqlConnection("server=xxx;uid=yyy;pwd=zzz;database=myerp;"))
    {
        sqlConn.Open();
        using (SqlCommand sqlComm = new SqlCommand("select libelle from dbo.vwArticlesPerm WHERE Ref = '" + ProdRef + "'", sqlConn))
            strResult = (string)sqlComm.ExecuteScalar();
        sqlConn.Close();
        return strResult;
    }
}

我给你的另一个建议是using(...)在实例化实现IDisposable接口的类时使用:当块类的执行退出被释放时using,这样你就可以避免内存浪费和一些头痛:)

于 2012-04-17T13:41:32.223 回答