一段完整的Silverlight演示代码[Web service]

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

namespace SilverlightInterview.Web
{
    /// <summary>
    /// StockService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class StockService : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetStock()
        {

            string[] stocks = { "万科","深发展","茅台","方欣科技","微软","联想科技"};

            Random r = new Random();


            string strTemp;
            string strReturn = @"<body>";
            for (int i = 0; i < stocks.Length; i++)
            {
                strTemp = "<stock>";
                strTemp += "<name>" + stocks[i] + "</name>";
                strTemp += "<value>" + (int)(r.NextDouble() * 70 + 30) + "</value>";
                strTemp += "<time>"+ System.DateTime.Now.ToString() +"</time>";
                strTemp += "</stock>";

                strReturn += strTemp;
            }

            strReturn += "</body>";

            return strReturn;
        }
    }
}

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

namespace SilverlightInterview.Web
{
    /// <summary>
    /// DataService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class DataService : System.Web.Services.WebService
    {

        string dbconn = "Server=WANGXIN;Database=Interview;Trusted_Connection=True";

        [WebMethod]
        public int AddNote(string topic, string remark)
        {
            string strSql = "";
            SqlConnection connection = new SqlConnection(dbconn);
            connection.Open();

            try
            {
                strSql = "insert into Ft_Note (topic,remark) values ('" + topic + "','" + remark + "')";

                SqlCommand cmd = new SqlCommand(strSql);
                cmd.Connection = connection;
                return cmd.ExecuteNonQuery();


            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }

        }

        [WebMethod]
        public int DelByID(string id)
        {
            string strSql = "";
            SqlConnection connection = new SqlConnection(dbconn);
            connection.Open();

            try
            {
                strSql = "delete from Ft_Note where id="+id ;

                SqlCommand cmd = new SqlCommand(strSql);
                cmd.Connection = connection;
                return cmd.ExecuteNonQuery();


            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }

        }

        [WebMethod]
        public List<Note> GetNotesByTopic(string topic)
        {
            string strSql = "";
            SqlConnection connection = new SqlConnection(dbconn);
            connection.Open();

            try
            {
                List<Note> notes = new List<Note>();

                if (topic == "*")
                {
                    strSql = "select id,topic,remark from Ft_Note ";
                }
                else
                {
                    strSql = "select id,topic,remark from Ft_Note where topic='"+topic+"' ";
                }

                SqlCommand com = connection.CreateCommand();
                com.CommandText = strSql;
                com.CommandType = System.Data.CommandType.Text;
                SqlDataReader dr = com.ExecuteReader(System.Data.CommandBehavior.Default);
                while (dr.Read())
                {
                    Note temp = new Note();
                    temp.ID = dr[0].ToString();
                    temp.Topic = dr[1].ToString();
                    temp.Remark = dr[2].ToString();
                    notes.Add(temp);
                 }
                return notes;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }

        }


    }
}

关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
原文地址:https://www.cnblogs.com/starcrm/p/1356474.html