Click here to Skip to main content
Click here to Skip to main content
Go to top

Custom Data Layer for ASP.NET With MySQL

, 13 Dec 2007
Rate this:
Please Sign up or sign in to vote.
Data access layer for MySQL based web projects.

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)

Share

About the Author

Shivanand Alagi
Software Developer
India India
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.

Comments and Discussions

 
QuestionHi Pingroupshriram20106-Sep-10 2:12 
Ranthi Pinmembervakti12-May-08 9:21 
GeneralRe: hi PinmemberShivanand Alagi23-Apr-09 21:11 
GeneralRe: hi PinmemberVivek Thakur25-May-09 9:07 

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.

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