首先我先说一下,如果真的要用linq做项目的话,也会是比较方便的。已经尝试了在三层架构中应用linq to sql 比较方便。
//有三个不同的数据库表,所以写法不一样
public class LinqTosql
{
public static GameCardSaleDataContext data = new GameCardSaleDataContext();
//输出新闻表所有标题
public void output()
{
foreach (var item in data.News)
{
Console.WriteLine(item.Title);
}
}
//根据ID查询一条数据
public void selectbyId(int id)
{
IEnumerable query = from m in data.News
where m.NewsId == id
select m;
foreach (News item in query)
{
Console.WriteLine(item.Content);
}
}
//插入一条数据 返回主键自增列ID
public int Insert(News news)
{
data.News.InsertOnSubmit(news);
data.SubmitChanges();
return news.NewsId;
}
//修改
public void Upade()
{
News news= data.News.Single(m => m.NewsId == 1);
news.Title = "你的标题被我修改了!";
data.SubmitChanges();//提交到数据库
}
//删除
public void delete()
{
News news = data.News.Single(m => m.NewsId == 1);
data.News.DeleteOnSubmit(news);
data.SubmitChanges();
}
}
public class LinqTosql
{
public static GameCardSaleDataContext data = new GameCardSaleDataContext();
//输出新闻表所有标题
public void output()
{
foreach (var item in data.News)
{
Console.WriteLine(item.Title);
}
}
//根据ID查询一条数据
public void selectbyId(int id)
{
IEnumerable query = from m in data.News
where m.NewsId == id
select m;
foreach (News item in query)
{
Console.WriteLine(item.Content);
}
}
//插入一条数据 返回主键自增列ID
public int Insert(News news)
{
data.News.InsertOnSubmit(news);
data.SubmitChanges();
return news.NewsId;
}
//修改
public void Upade()
{
News news= data.News.Single(m => m.NewsId == 1);
news.Title = "你的标题被我修改了!";
data.SubmitChanges();//提交到数据库
}
//删除
public void delete()
{
News news = data.News.Single(m => m.NewsId == 1);
data.News.DeleteOnSubmit(news);
data.SubmitChanges();
}
}
//上文来自CSDN博客:http://blog.csdn.net/zhoukang0916/archive/2009/10/14/4669743.aspx
//----Linq to sql + gridview
FDCJYDataContext fdc = new FDCJYDataContext();
public void Bind()
{
gvwMessage.DataSource = from m in fdc.Message where m.UserName == Session["user"].ToString() select m;
gvwMessage.DataKeyNames = new string[] { "ID" };
gvwMessage.DataBind();
}
protected void gvwMessage_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ss = gvwMessage.DataKeys[e.RowIndex].Value.ToString();
var message = fdc.Message.Single(m => m.ID == int.Parse(ss));
fdc.Message.DeleteOnSubmit(message);
fdc.SubmitChanges();
Bind();
}
protected void gvwMessage_RowEditing(object sender, GridViewEditEventArgs e)
{
gvwMessage.EditIndex = e.NewEditIndex;
Bind();
}
//修改(更新):不同于上面数据库
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//更新
{
var zhi = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
Tb_love ff=love.Tb_love.Single(b=>b.ID==(zhi));
ff.name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].FindControl("tex_name"))).Text.ToString().Trim();
ff.age = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].FindControl("tex_age"))).Text.ToString().Trim());
ff.love = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].FindControl("tex_love"))).Text.ToString().Trim();
love.SubmitChanges();
GridView1.EditIndex = -1;
bind();
}