陈斌彬的技术博客

Stay foolish,stay hungry

iOS TableView 获取服务端数据

数据库设计

img

img

启动调试:

img

img

核心程序:

服务端

img

客户端

img

C#代码实现:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;

namespace ReadHomework
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </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 WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        [XmlInclude(typeof(HomeWorkClass))]
        public IList GetTableList()
        {
            //连接SQL数据库
            System.Data.SqlClient.SqlConnection SqlCnn = new System.Data.SqlClient.SqlConnection("Data Source=JOHN;Initial Catalog=webservice;User ID=sa;Password=12345678;");
            //打开数据库连接
            SqlCnn.Open();
            //加入SQL语句,实现数据库功能
            System.Data.SqlClient.SqlDataAdapter SqlDa = new System.Data.SqlClient.SqlDataAdapter("select * from dbo.homework", SqlCnn);
            //创建缓存
            DataSet DS = new DataSet();
            //将SQL语句放入缓存
            SqlDa.Fill(DS);
            //获取第一张表
            DataTable dt = DS.Tables[0];
            IList result = new ArrayList();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int title = (int)dt.Rows[i][0];
                string content =(string)dt.Rows[i][1] ;
                DateTime time = (DateTime)dt.Rows[i][2];
                result.Add(new HomeWorkClass(title, content, time.ToString("yyyy-MM-dd")));
            }
            return result;
        }

        public class HomeWorkClass
        {
            private int title;
            private string content;
            private string time;

            public HomeWorkClass()
            {

            }
            public HomeWorkClass(int title, string content, string time)
            {
                this.title = title;
                this.content = content;
                this.time = time;
            }

            public int Title
            {
                get { return title; }
                set { title = value; }
            }
            public string Content
            {
                get { return content; }
                set { content = value; }
            }
            public string Time
            {
                get { return time; }
                set { time = value; }
            }
        }


    }
}

Resource Reference