Database
  Home arrow Database arrow Page 4 - Database Programming in C# with MySQL : Us...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Database Programming in C# with MySQL : Using OleDB
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2007-06-18

    Table of Contents:
  • Database Programming in C# with MySQL : Using OleDB
  • OleDB continued
  • Accessing MySQL, Step by Step
  • MySQL Access in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Database Programming in C# with MySQL : Using OleDB - MySQL Access in the Real World


    (Page 4 of 4 )

    The example I will be developing will primarily focus on a class that returns OleDbConnection, OleDbCommand and OleDbDataReader instances. It contains two classes: Data, which creates and returns instances of OleDbConnection, OleDbCommand and OleDbDataReader; and DataTest, which tests the functionalities provided by the Data class.

    Let's start with the Data class. Its parameterized constructor creates the connection string from the parameters passed and instantiates the OleDbConnection connection class using the string. The getDataReader method returns an OleDbDataReader instance based on the OleDbCommand instance passed. Here is the class:

    using System;
    using System.Data;
    using System.Data.OleDb;

    namespace MySQLApp
    {
       /// <summary>
      
    /// Creates and returns OleDbConnection, OleDbCommand
       /// and OleDbDataReader
       /// </summary>
       public class Data
       {
         
    private OleDbConnection connection=null;
         private OleDbCommand command=null;
         string connectionString=null;

         public Data()
         {
           connectionString="";
         }

         public Data(string host, string userId, string password)
         {
          
    connectionString=" Provider=MySqlProv; Data Source="+host+"; User id="+userId+"; Password="+password+";";

           connection=new OleDbConnection(connectionString);
           command=new OleDbCommand();
         }

         public OleDbCommand getCommand(string sqlString)
         {
           command.Connection=connection;
           command.CommandText=sqlString;
           return command;
         }

         public OleDbDataReader getReader(OleDbCommand command)
         {
           return command.ExecuteReader();
         }
       }
    }

    The next class is DataTest. It creates an instance of the Data class and executes a SQL statement using it. Here is the code:

    using System;

    namespace MySQLApp
    {
       /// <summary>
       ///Creates an instance of Data class to execute a
       ///simple SQL statement.
       /// </summary>
       class DataTest
       {
         /// <summary>
         /// The main entry point for the application.
         /// </summary>
         [STAThread]
         static void Main(string[] args)
         {
           Data data=new Data("localhost","root","root123");
           data.getReader(data.getCommand("select * from 
            user"));

             while( reader.Read())
             Console.WriteLine(reader.GetString(1));

         }
       }
    }

    That brings us to the end of this discussion. The application developed here will form the basis of advanced operations using DataAdapter in the next part of this discussion. Till then…


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this article I have discussed about using OleDB Data Provider to access MySQL...
       · But in this Article i m getting this error"The 'MySqlProv' provider is not...
       · Nice described in the article. Keep going on!If you would have used ODBC instead...
       · me too getting the same error. some one please let me know the cause for this?
       · Why is the verb "store" not acceptable? "Persist" is an intransitive verb, and...
     

    DATABASE ARTICLES

    - Converting Your Excel Worksheet into a Worki...
    - Excel Reference
    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT