zoukankan      html  css  js  c++  java
  • 一段完整的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
  • 相关阅读:
    SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问
    谷歌浏览器扩展程序manifest.json参数详解
    获取天气api
    UVA 10385 Duathlon
    UVA 10668 Expanding Rods
    UVALIVE 3891 The Teacher's Side of Math
    UVA 11149 Power of Matrix
    UVA 10655 Contemplation! Algebra
    UVA 11210 Chinese Mahjong
    UVA 11384 Help is needed for Dexter
  • 原文地址:https://www.cnblogs.com/starcrm/p/1356474.html
Copyright © 2011-2022 走看看