0

VS と C# の完全な初心者です。非常に単純なことから始めて、Web サービスをセットアップしようとしています。アイテムのコードが与えられると、アイテムの名前を (うまくいけば) 返すクラスを書くことができました。これを実装して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;
        }
    }

Web サービス コード:

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

私があなたに与えるもう1つのアドバイスは、インターフェイスusing(...)を実装するクラスをインスタンス化するときに使用することです:ブロッククラスから実行が終了すると、クラスが破棄されるため、メモリの浪費と頭痛を避けることができます:)IDisposableusing

于 2012-04-17T13:41:32.223 に答える