|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article will give you an overview how to make am asp.net 3.5 website using LINQ. This is very easy and simple way to build a web site using LINQ because this will provide you SQL classes. Step By StepHere are the steps and procedure to make your application.
Insert, delete, update methods for your application Now it’s time to make your database helper class. Take a class file names DatabaseHelper.cs To get DataContext of you current database write a method GetDatabaseDataContext() public const string ConnectionStringName = "TestDBConnectionString"; public static TestDatabaseDataContext GetDatabaseData() { var db = new TestDatabaseDataContext(ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString); return db; } Lets Write a generic insert method to insert data in table
public static bool Insert<T>(T obj) where T : class { using (var db = GetDatabaseData()) { db.GetTable<T>().InsertOnSubmit(obj); db.SubmitChanges(); return true; } } This is the generic update method to update data in tables public static void Update<T>(T obj, Action<T> update) where T : class { using (var db = GetDatabaseData()) { db.GetTable<T>().Attach(obj); update(obj); db.SubmitChanges(); } } Here is the generic delete method to delete data from table public static void Delete<T>(T obj) where T : class, new() { using (var db = GetDatabaseData()) { db.GetTable<T>().Attach(obj); db.GetTable<T>().DeleteOnSubmit(obj); db.SubmitChanges(); } } So your database helper class is ready to make insert, delete, update operation with any table in your Datatabse which you have enter in you DataContext. Here i am showing you one example of how to call these methods from your web page so you can use these DatabaseHelper class perfectly. Call database methods from web pages Here to show how to use these DatabaseHelper methods I am using a sample table tblUser . To get data from user you will write a very simple query look like that public tblUser GetUserById(int Id) { using (var db = DatabaseHepler.GetDatabaseData()) { var user = (from e in db.tblUsers where e.ID == Id select e).FirstOrDefault(); return user; } } Now to insert data in your table call a method InsertUser(tblUser user), here parameter is coming with new data of user and will insert this data to table. Here is the code to call Databasehelper Insert Method. public void InsertUser(tblUser user) { DatabaseHepler.Insert<tblUser>(user); }Code for Update and delete data are public void UpdateUser(tblUser user) { DatabaseHepler.Update<tblUser>(user, delegate(tblUser t) { t.ID = user.ID; t.UserName = user.UserName; t.UserPass = user.UserPass; }); } public void DeleteUser(int Id) { tblUser user = GetUserById(1); DatabaseHepler.Delete<tblUser>(user); } So you are now prepare to build your website using in ASP.NET 3.5 using LINQ. It is easy and you can write your code faster. Points of InterestAs i am working one of my project in ASP.NET 3.5 and using LINQ I am enjoying the latest technology. I feel every developer can enjoy such interesting programming so sharing my basic idea on it. Happy coding :) HistoryJust sharing some basic ideas so you become interested to work in VS2008 and LINQ. As I am already worlking on it and enjoying more to work than VS 2005.
|
||||||||||||||||||||||||||||||||||||||||