The old ADO.Net (sqlConnection, etc.) is a dinosaur with the advent of LINQ. LINQ requires .Net 3.5, but is backward compatible with all .NET 2.0+ and Visual Studio 2005, etc.
Getting started with linq is ridiculously easy.
- Add a new linq-to-sql element to the project that will be placed in your App_Code folder (in this example we will call this example.dbml )
- from your server explorer, drag the table from your database to dbml (the table will be called items in this example)
- save dbml file
Now you have created several classes. You created the exampleDataContext class, which is your linq initializer, and you created the item class, which is the class for objects in the items table. All this is done automatically, and you do not need to worry about it. Now say that I want to get an entry with itemID of 3, thatโs all I need to do:
exampleDataContext db = new exampleDataContext(); // initializes your linq-to-sql item item_I_want = (from i in db.items where i.itemID == 3 select i).First(); // using the 'item' class your dbml made
And thatโs all it takes. Now you have a new item named item_I_want ... now, if you want to get some information from the item , you simply call it like this:
int intID = item_I_want.itemID; string itemName = item_I_want.name;
Linq is very easy to use! And this is just the tip of the iceberg.
No need to learn legacy ADO when you have a more powerful and simpler tool at your disposal :)
naspinski
source share