您的位置:首页 > 数据库

LINQ to SQL [综合]

2013-09-22 10:31 141 查看
最近学习 linq to sql 感觉还是可以的,方便快捷,以前问是以为不好用,不想用,但是今天用了一下感觉还是不错的, 确实是快,但是我又在想如果出错了怎么办,这样一个类里放那么多的文件是有点不好,再说了 linq to sql 低层不知道怎么实现的,还是没有自己写的访问感觉舒服点呵呵!!!如果是快速开发用 linq to sql 还是不错的选择啊!!!-基本的增删除改查

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;

}
}
}
1. TCDataClasses1DataContext tc = new TCDataClasses1DataContext();
tc.ExecuteCommand("update table set sceState = 9 where sceState = 7");可以真写语句方便
2.TCDataClassesDataContext tc = new TCDataClassesDataContext(); tc.PGetAccountBalance(ofid).ToList()[0]
可以直接用存储过程的名称调用也很方便,呵呵。
大家有什么经验顶顶吧


http://www.sufeinet.com/forum.php?mod=viewthread&tid=129
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: