Click here to Skip to main content
Licence CPOL
First Posted 13 Dec 2007
Views 24,430
Downloads 999
Bookmarked 37 times

Custom Data Layer for ASP.NET With MySQL

By | 13 Dec 2007 | Article
Data access layer for MySQL based web projects.
 
Part of The SQL Zone sponsored by
See Also

Introduction

Have you ever used a vague combination such as ASP.NET (2.0) and a database like MySQL? In this project, you would get data access functionality for MySQL that would help you to minimize your efforts to write a Data Access Layer in your Web Projects while using such a combination.

Background

The MySql.Data namespace consists most of the required classes. This namespace have some methods in the classes which do not accommodate all features such as ExecuteDataset, and some of them, say, MySqlHelper, do not support Stored Procedures with parameters. In this project, the data access layer would contain methods such as ExecuteDataset, ExecuteNonQuery, ExecuteScalar, and ExecuteReader, which would use Stored Procedures and queries, both with and without parameters.

Using the code

Here is how to use the data access class in the code. For detailed implementation, please go through the demo project. Set the connection string appropriately in the web.config file.

/// This method demonstrates ExecuteDataset.
/// Passing CommandType as Store Procedure & CommandText
/// as Store Procedure Name.

DataSet dsEmployee = new DataSet();
dsEmployee = DataAccess.ExecuteDataSet(CommandType.StoredProcedure, 
                        "EmployeeDetails");

/// This method demonstrates ExecuteDataset.
/// Passing CommandType as Store Procedure & CommandText
/// as Store Procedure Name And Parameters.

DataSet dsEmployee = new DataSet();

MySqlParameter[] mySqlParams = new MySqlParameter[1];
mySqlParams[0] = new MySqlParameter();
mySqlParams[0].ParameterName = "?EmpId";
mySqlParams[0].Value = 2;

dsEmployee = DataAccess.ExecuteDataSet(CommandType.StoredProcedure, 
             "EmployeeDetails",mySqlParams);

/// This method demonstrates ExecuteDataset.
/// Passing CommandType as Text & CommandText as Query And Parameters.

DataSet dsEmployee = new DataSet();

MySqlParameter[] mySqlParams = new MySqlParameter[1];
mySqlParams[0] = new MySqlParameter();
mySqlParams[0].ParameterName = "?EmpId";
mySqlParams[0].Value = 2;

dsEmployee = DataAccess.ExecuteDataSet(CommandType.Text, 
  "Select * from employee where Id =?EmpId", mySqlParams);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shivanand Alagi

Software Developer

India India

Member

Having 3.5 years of work experience in software development.
 
Good knowledge of software development lifecycle, active involvement in development, testing and support.
 
Earned several Dot Net certified credentials from Brian Bench and Microsoft like MCPD, MCTS MCAD.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
QuestionHi Pingroupshriram20102:12 6 Sep '10  
Ranthi Pinmembervakti9:21 12 May '08  
GeneralRe: hi PinmemberShivanand Alagi21:11 23 Apr '09  
GeneralRe: hi PinmemberVivek Thakur9:07 25 May '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 14 Dec 2007
Article Copyright 2007 by Shivanand Alagi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid