none
linq to sql增查删改求具体步骤 RRS feed

  • 问题

  • linq to sql怎么将查询到的内容显示在gridview中   怎么用linq 删除数据库中的数据。最后有具体步骤,对这个一点都不懂。
    2012年7月18日 2:58

答案

  • 试试看下列的代码:

    /// <summary>
    /// 刪
    /// </summary>
    private static void Delete()
    {
    	using (DataClasses1DataContext context = new DataClasses1DataContext())
    	{
    		var query = from p in context.Regions
    					where p.RegionID == 5
    					select p;
    		if (query.Any())
    		{
    			context.Regions.DeleteAllOnSubmit(query);
    			context.SubmitChanges();
    		}
    	}
    }
    
    /// <summary>
    /// 改
    /// </summary>
    private static void Update()
    {
    	using (DataClasses1DataContext context = new DataClasses1DataContext())
    	{
    		var query = from p in context.Regions
    					where p.RegionID == 5
    					select p;
    		if (query.Any())
    		{
    			query.First().RegionDescription = "aaa";
    			context.SubmitChanges();
    		}
    	}
    }
    
    //增
    private static void Insert()
    {
    	using (DataClasses1DataContext context = new DataClasses1DataContext())
    	{
    		Region r = new Region();
    		r.RegionID = 5;
    		r.RegionDescription = "test";
    		context.Regions.InsertOnSubmit(r);
    		context.SubmitChanges();
    	}
    }
    
    /// <summary>
    /// 查
    /// </summary>
    private void Query()
    {
    	using (DataClasses1DataContext context = new DataClasses1DataContext())
    	{
    		var query = from p in context.Regions
    					select p;
    		GridView1.DataSource = query;
    		GridView1.DataBind();
    	}
    }


    以上說明若有錯誤請指教,謝謝。
    http://www.dotblogs.com.tw/terrychuang/

    2012年7月18日 6:46