[C#] 纯文本查看 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace WebServiceText
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
DataClasses1DataContext context = new DataClasses1DataContext();
[WebMethod]
public List<Text> selectSwhere(int startid,int endid)
{
var result = (from text in context.Text
where text.id >= startid && text .id <=endid
select text).ToList<Text>();
return result;
}
[WebMethod]
public List<Text> selectAll()
{
return context.Text.ToList<Text>();
}
/// <summary>
/// 添加一个数据
/// </summary>
/// <param name="sex">性别</param>
/// <param name="age">年龄</param>
/// <returns>成功为True否则为False</returns>
[WebMethod]
public List<Text> addText(string sex, int age)
{
Text text = new Text();
text.age = age;
text.sex = sex;
//context.Text.First<Text>();
// 使用Sql方式查询
//var result = (from text in context.Text
// select text).ToList<Text>();
// 提交一个插入
context.Text.InsertOnSubmit(text);
// 更新数据库
context.SubmitChanges();
var result = context.Text.ToList<Text>();
return result;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="id">根据ID做删除</param>
/// <returns>成功为True否则为False</returns>
[WebMethod]
public List<Text> deleterText(int id)
{
// 删除
var text1 = (from t in context.Text
where t.id == id
select t).First<Text>();
context.Text.DeleteOnSubmit(text1);
context.SubmitChanges();
var result = context.Text.ToList<Text>();
return result;
}
/// <summary>
/// 修改Text
/// </summary>
/// <param name="id">编号</param>
/// <param name="sex">性别</param>
/// <param name="age">年龄</param>
/// <returns>成功为True否则为False</returns>
[WebMethod]
public List<Text> updateText(int id, string sex, int age)
{
// 更新
var text = (from t in context.Text
where t.id == id
select t).First<Text>();
text.age = age;
text.sex = sex;
context.SubmitChanges();
var result = context.Text.ToList<Text>();
return result;
}
/// <summary>
/// 查询
/// </summary>
/// <param name="Swhere">条件</param>
/// <returns>返回一个表</returns>
[WebMethod]
public List<Text> selectText(int id)
{
//使用Sql方式查询
var result = (from text in context.Text
where text.id == id
select text).ToList<Text>();
return result;
}
}
}