Developing ASP.NET applications that work with data in Microsoft SQL Server can be daunting, even for professional developers. The idea of developing web applications has always been separating the code from the underlying data structure. While Microsoft’s declared that Entity Framework is the way they’re going, LINQ to SQL classes still remains an extremely fast and flexible method of object-relational mapping in .NET. It’s also a very good way for simplifying your own development efforts.
Here we have a simple form you might need to develop that permits you to insert information into a SQL database. Notice when we click the submit button that we get a confirmation message that also returns the unique identity of the record inserted into the database.
LINQ to SQL Classes
Knowing nothing more than the connection name, you can add a new LINQ to SQL classes item by dragging tables straight from that connection object onto the .dbml file that’s created. Addition of this file not only adds a connection string value to the web.config automatically for you, it also provides you with a base constructor that makes a default call to the DataContext class. The following screenshot shows the code you can then write to add the record to the database.
Notice LINQ to SQL allows you to store the context in a using statement (as you would store a SQL connection object). Also notice LINQ to SQL also derives each entity (database table) as a class! This means LINQ to SQL removes having to write separate classes for each entity. It also takes care of all the create, read, update and delete methods so you don’t have to write stored procedures. Finally, you can add stored procedures to the LINQ to SQL classes ORM file, as well. You can then treat these as methods.
Having had to design and build many databases over the years, as well as write all the code, I very much appreciate LINQ to SQL. It has saved me a lot of time and, believe it or not, I think you will find it does the same for you. So take the risk and try it out.
God bless and happy coding!
Speak your mind...