Linq to Sql學習

雖然接觸微軟的東西時間不長,但對於orm這塊,自己認爲 Linq to sql 比起hibernate 要有優勢,對於中小型系統,它會更活躍。不要去管微軟要不要放棄 Linq to sql ,它現在能滿足我的需求,而且好用,這就足夠了。微軟是不可能放棄對它的支持的。

出處:http://www.cnblogs.com/greatverve/archive/2010/05/13/1734236.html 

                                                        尊敬知識,尊敬作者是種美德

1.接上篇http://www.cnblogs.com/greatverve/archive/2010/05/12/1733513.html
在Northwind下新建表t_User
ID,             int,主鍵,自增。
UserName,  varchar(50)
Pwd,           varchar(50)

2.從服務器資源管理器中把t_User拖到Northwind.dbml中。
即生成orm映射。

3.增刪改查的代碼參考下面的Linq3();

複製代碼
Linq <wbr>to <wbr>Sql學習代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.Common;
using System.Collections.Generic;

namespace FirstLinq
{
    
public partial class _Default : System.Web.UI.Page
    {
        NorthwindDataContext ctx 
= new NorthwindDataContext("Data Source=ZHANGSHUQIANG;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa");
        
protected void Page_Load(object sender, EventArgs e)
        {
            Linq1();
            Linq2();
            Linq3();
        }
        
/// 
        
/// 增刪改查
        
/// 
        private void Linq3()
        {
            
//
            t_User user = new t_User();
            user.UserName 
= "大氣象";
            user.Pwd 
= "123456";
            ctx.t_User.InsertOnSubmit(user);
//以前的方法是Add();
            ctx.SubmitChanges();

            
//
            
//參考這樣的語法string s = (from a in ctx.Customers select a).Single(a => a.ID == 2);
            t_User userUpdate = ctx.t_User.SingleOrDefault(t_User => t_User.ID == 2);//Single與SingleOrDefault沒區別
            userUpdate.UserName = "大氣象1";
            ctx.SubmitChanges();

            
//
            t_User userDelete = (from userinfo in ctx.t_User where userinfo.ID == 1 select userinfo).FirstOrDefault();
            
if (userDelete != null)
            {
                ctx.t_User.DeleteOnSubmit(userDelete);
                ctx.SubmitChanges();
            }
        }
        
/// 
        
/// 熟悉Linq to sql語法
        
/// 
        private void Linq2()
        {
            Customers customer 
= new Customers();

            
//執行普通的sql語句,查詢CustomerID="ANATR"的記錄
            IEnumerable<</span>Customers> customers = ctx.ExecuteQuery<</span>Customers>("select * from Customers where CustomerID='ANATR'");
            customer 
= customers.First();
            Response.Write(customer.CustomerID);

            
//使用Linq查詢單條記錄
            var cus = from c in ctx.Customers where c.CustomerID.Equals("ANATR") select c;
            customer 
= cus.First();
            Response.Write(customer.CompanyName);

            
//查詢結果集,語法:from 臨時表名 in 表集合 orderby 臨時表名.字段名 升級序 select 臨時表名
            gdvCustomers.DataSource = from cust in ctx.Customers where cust.CustomerID != "ANATR" orderby cust.CompanyName descending select cust;
            gdvCustomers.DataBind();
        }
        
/// 
        
/// 熟悉一下linq語法,變量的定義類似js,無須聲明類型。
        
/// 
        private void Linq1()
        {
            var age 
= 29;
            var username 
= "greatverve";
            var userlist 
= new[] { "a""b""c" };
            
foreach (var user in userlist)
            {
                Response.Write(user);
            }
        }
    }
}
複製代碼

 

 

參考:
http://www.cnblogs.com/webabcd/archive/2007/10/18/928353.html
http://www.cnblogs.com/lovecherry/archive/2007/08/16/858009.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章