Share/Save/Bookmark

Friday 10 April 2009

LinQ with SQL

by Your Name comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

LinQ is a new technical solution of .NET 3.5, you can use LinkQ to replace data access layer and entities class in your application architecture.

- LinQ has created entities class for you.
- LinQ has insert, update and delete method
- LinQ help you to call procedure easier

When you create a new LinQ object and then drag tables and views into LinQ, it will auto generates entities class for you, insert, update and delete method also has been create, you just focus on create your business layer in you application architecture

You also can drag procedures or functions in LinQ object to call it in your business layer

An example of using LinQ with SQL 2005:

I have a database server name Finance it has a table Credit Card
Table Credit Card has two columns Card No and Card Type

I created an LinQ object named Finance and drag Credit Card table in it

Now I can use entity class name Credit Card to assign parameter from UI layer to insert, update and delete into Finance database

There are insert, update and delete methods I created in business layer (I use C# in this code)

Private void Insert(string cardNo, string cardType){
FinanceDataContext cardObject = new FinanceDataContext();
CreditCard creditCard = new CreditCard();
creditCard.CardNo = cardNo;
creditCard.CardType = cardType;
cardObject.CreditCards.InsertOnSubmit(creditCard);
cardObject.SubmitChanges();
}

Private void Update(string cardNo, string cardType){
FinanceDataContext cardObject = new FinanceDataContext();
var creditCard = (From c in cardObject.CreditCards
Where c.CardNo = cardNo
Select c).Single;
creditCard.CardType = cardType;
cardObject.SubmitChanges();
}

Private void Delete(string cardNo){
FinanceDataContext cardObject = new FinanceDataContext();
var creditCard = (From c in cardObject.CreditCards
Where c.CardNo = cardNo
Select c).Single;
cardObject.CreditCards.DeleteOnSubmit(creditCard);
cardObject.SubmitChanges();
}




Categories

Labels

Archive