Sunday, February 15, 2009

Using LINQ in ASP.NET

I have a new book: C# 2008 by Mei-Weng Lee, WROX. I am playing around with LINQ in ASP.NET. The first step was to remember how to access SQL Server and SQL Server Express; I had already installed both of these some years ago. When I opened the Server Explorer in VS08, the Data Connections was empty.

I followed the instructions in the book to download pubs and Northwind databases, although I was sure I already had Northwind installed. The key step was to run the installation scripts from a VS08 command prompt.

sqlcmd -S .\SQLEXPRESS -i installpubs.sql
sqlcmd -S .\SQLEXPRESS -i installnwnd.sql

After returning to the Server Explorer, I still did not have any data connections, so I added one. I used SQL Server as the connection type; I chose SQLEXPRESS as the server; I chose the pubs database. Now I have a connection.

I used the visual designer for the .dbml file and dragged two tables onto it. After saving, the designer file had created a class with declared properties for accessing the tables.

I had to start again: at first, I selected new project, instead of new web site. Much better as a web site, I am now seeing all the prompts that I am supposed to see. After I understand this better, I will need to analyze the difference between a project and a web site.

I was able to access the table from ASP. I see that LINQ can create classes based on tables, but can it create tables based on classes? I have not seen any information on this. The other problem I have is that a lot of code is written in the ASP and not in a separate controller for the business logic.

I will look into using Hibernate with .NET. I will also look into moving all the business logic into one code-behind file and put all the validation in the data class.

No comments:

Post a Comment

Followers