Tuesday, March 4, 2014

Create a DLL shared by projects with asp.net and C#

Background

After having finished several projects for Graduate School, I noticed that there are a lot of duplicate code, especially the code for operating the database. The following is a typical code segment for inquiring a table in the database:




My Solution

To reduce the duplicate code, I created a DLL named DatabaseOperation which serves as a middle layer and encapsulates all the details of operating a database. The only thing users need is to provide correct sql and pass the sql to DBOperation. Example code is as follows:

 

The way to create a DLL and use it from a project

In Visual Studio 2012, File->New->Project  Choose Template/Visual C# on the left hand side, then select "Class Library" from all the project types listed at the right hand side.

Add .cs files into the project. Build the project. In the local find, locate a folder with the name "bin", you will find the dll file within the debug folder.

Open the project in VS 2012, open a project, right click on the project full name, choose "Add Reference" and browse the dll file at local drive. The dll will be integrated with the project and be uploaded to the "Bin" folder in the server.

No comments:

Post a Comment