客户端: 服务器端:
1 create table CourseVideo 2 ( 3 VideoID int IDENTITY(1,1) NOT NULL, 4 CourseID int NOT NULL, 5 VideoName varchar(500) NULL, 6 VideoPath [varchar](100) NULL, 7 VideoImage [varchar](200) NULL, 8 VideoDes [varchar](200) NULL, 9 VideoLength int NULL, 10 primary key(VideoID) 11 )
添加数据
INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (1, 1, N'数组1_为什么要使用数组.mp4', N'CourseVideo/1.mp4', N'CourseImage/1.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (2, 1, N'数组2_什么是数组', N'CourseVideo/2.mp4', N'CourseImage/2.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (3, 1, N'数组3_数组的分类及特点', N'CourseVideo/3.mp4', N'CourseImage/3.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (4, 1, N'数组4:一维数组的声明', N'CourseVideo/4.mp4', N'CourseImage/4.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (5, 1, N'数组5:一维数组的初始化', N'CourseVideo/5.mp4', N'CourseImage/5.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (6, 1, N'数组6:一维数组的访问(引用)', N'CourseVideo/6.mp4', N'CourseImage/6.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (7, 1, N'数组7:数组的算法:查找', N'CourseVideo/7.mp4', N'CourseImage/7.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (8, 1, N'数组8:数组的算法:排序 (1)', N'CourseVideo/8.mp4', N'CourseImage/8.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (9, 1, N'数组09:二维数组的声明', N'CourseVideo/9.mp4', N'CourseImage/9', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (10, 1, N'数组10:二维数组的初始化', N'CourseVideo/10.mp4', N'CourseImage/10.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (11, 1, N'数组11:二维数组的访问', N'CourseVideo/11.mp4', N'CourseImage/11.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (12, 1, N'数组12:多维数组简介', N'CourseVideo/12.mp4', N'CourseImage/12.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (13, 1, N'数组13:C语言中的字符串', N'CourseVideo/13.mp4', N'CourseImage/13.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (14, 1, N'数组14:字符数组', N'CourseVideo/14.mp4', N'CourseImage/14.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (15, 1, N'数组15:字符串的输入输出', N'CourseVideo/15.mp4', N'CourseImage/15.png', NULL) GO INSERT [dbo].[CourseVideo] ([VideoID], [CourseID], [VideoName], [VideoPath], [VideoImage], [VideoDes]) VALUES (16, 1, N'数组16:字符串处理函数', N'CourseVideo/16.mp4', N'CourseImage/16.jpg', NULL) GO
public class DatableToList { public static List<T> ConvertToList<T>(DataTable dt) where T : new() { //定义集合 List<T> ts = new List<T>(); //获得此模型的类型 Type type = typeof(T); //定义一个临时变量 string tempName = string.Empty; //便利DataTable数据行 foreach (DataRow dr in dt.Rows) { T t = new T(); //获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); //遍历该对象的所有属性 foreach(PropertyInfo pi in propertys) { tempName = pi.Name;//将属性名称赋值给临时变量 //检查DataTable是否包含此列(列名==对象的属性名) if (dt.Columns.Contains(tempName)) { // 判断此属性是否有Setter if (!pi.CanWrite) continue;//该属性不可写,直接跳出 //取值 object value = dr[tempName]; //如果非空,则赋给对象的属性 if (value != DBNull.Value) pi.SetValue(t, value, null); } } //对象添加到泛型集合中 ts.Add(t); } return ts; } }
CourseVideo.cs类
public class CourseVideo { private int videoID; public int VideoID { get { return videoID; } set { videoID = value; } } private int courseID; public int CourseID { get { return courseID; } set { courseID = value; } } private String videoName; public String VideoName { get { return videoName; } set { videoName = value; } } private String videoPath; public String VideoPath { get { return videoPath; } set { videoPath = value; } } private String videoImage; public String VideoImage { get { return videoImage; } set { videoImage = value; } }
private int videoLength;
public int VideoLength { get { return videoLength; } set { videoLength = value; } }
}
CourseVideoDal.cs类
查询CourseVideo表信息
这是比较简单容易理解的方式,但是字段多的话就很不实用。
//查询视频资源 public List<CourseVideo> Select() { List<CourseVideo> list = new List<CourseVideo>(); DataTable dt = new DataTable(); CourseVideo model = null; DataBase db = new DataBase(); String comstr = "select VideoID,CourseID,VideoName,VideoPath,VideoImage,VideoLength from CourseVideo"; dt = db.GetDataTable(comstr); for (int i = 0; i < dt.Rows.Count;i++ ) { model = new CourseVideo(); model.VideoID = Convert.ToInt32(dt.Rows[i][0]); model.CourseID = Convert.ToInt32(dt.Rows[i][1]); model.VideoName = dt.Rows[i][2].ToString(); model.VideoPath = dt.Rows[i][3].ToString(); model.VideoImage = dt.Rows[i][4].ToString(); model.VideoLength = Convert.ToInt32(dt.Rows[i][5]); list.Add(model); } return list; }
之前新建的DatableToList.cs类文件就可以用到了,使用泛型将DataTable数据转换为List<T>
泛型之前一直没机会用到,于是自己百度学习了一下,封装好DatableToList文件后,很好的提高代码
了质量,更方便使用。
//使用泛型 查询视频资源 public List<CourseVideo> Select2() { List<CourseVideo> list = new List<CourseVideo>(); DataTable dt = new DataTable(); DataBase db = new DataBase(); String comstr = "select VideoID,CourseID,VideoName,VideoPath,VideoImage,VideoLength from CourseVideo"; dt = db.GetDataTable(comstr); list = DatableToList.ConvertToList<CourseVideo>(dt); return list; }
只需要在App_Code文件夹下找到Service.cs添加
添加命名空间:
using System.Web.Script.Services;using System.Web.Script.Serialization;
还有在方法前面声明
[WebMethod(Description = " json查询视频资源 " )]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod(Description = "json查询视频资源")] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public String VideoSelect() { return new JavaScriptSerializer().Serialize(courseVideoDal.Select()); // return dal.Select(); }
[WebMethod(Description = "xml查询视频资源")] public List<CourseVideo> xmlVideoSelect() { return courseVideoDal.Select(); }
设置外部访问需要在Web.config添加节点:
<webServices> <protocols> <add name="HttpSoap" /> <add name="HttpPost" /> <add name="HttpGet" /> <add name="Documentation" /> </protocols> </webServices>
看下结果
接下来把项目部署在IIS服务器上即可使用,如何部署我这就不多说了,可以查一下百度
//视频ID @property (assign,nonatomic) int ID; //视频名称 @property (copy,nonatomic) NSString *name; //点击次数: 更新时间:2015-06-29 【打印此页】 【关闭】