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);
}
}