Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / January 2006

Tip: Looking for answers? Try searching our database.

How to write generic code access in ADO.NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
anonieko@hotmail.com - 30 Jan 2006 12:38 GMT
M. Chand is a .NET consultant, author and the admin and founder of C#
Corner. He has been working with .NET technology since pre beta
releases.

http://www.dotnetwire.com/frame_redirect.asp?newsid=3171

using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using Microsoft.Data.Odbc;

namespace GenericDataAccessApp
{
   public class GenericAdoNetComp
   {
       private IDbConnection idbConn = null;
       private IDbDataAdapter idbAdapter = null;
       private DbDataAdapter dbAdapter = null;
       private IDataReader iReader = null;

       public GenericAdoNetComp()
       {
       }

       // GetConnection returns IDbConnection
       public IDbConnection GetConnection(int connType,
       string connString)
       {
           switch (connType)
           {
               case 1: // OleDb Data Provider
                   idbConn = new OleDbConnection(connString);
                   break;
               case 2: // Sql Data Provider
                   idbConn = new SqlConnection(connString);
                   break;
               case 3: // ODBC Data Provider
                   idbConn = new OdbcConnection(connString);
                   break;
               // case 3: // Add your custom data provider
               default:
                   break;
           }
           return idbConn;
       }

       // GetDataAdapter returns IDbDataAdapter
       public IDbDataAdapter GetDataAdapter(int connType,
       string connString, string sql)
       {
           switch (connType)
           {
               case 1: // OleDb Data Provider
                   idbAdapter = new OleDbDataAdapter(sql, connString);
                   break;
               case 2: // Sql Data Provider
                   idbAdapter = new SqlDataAdapter(sql, connString);
                   break;
               case 3: // ODBC Data Provider
                   idbAdapter = new OdbcDataAdapter(sql, connString);
                   break;
               // case 3: // Add your custom data provider
               default:
                   break;
           }
           return idbAdapter;
       }
   }

}

public class Client
{
   private void ConnectBtn_Click(object sender, System.EventArgs e)
   {
       GenericAdoNetComp genDP = new GenericAdoNetComp();
       sql = "SELECT * FROM Employees";

       if (radioButton1.Checked)
       {
           connString =
           "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\\Northwind.mdb";
           conn = genDP.GetConnection(1, connString);
           adapter = genDP.GetDataAdapter(1, connString, sql);
       }
       else if (radioButton2.Checked)
       {
           connString =
           "Data Source=MCB;Initial Catalog=Northwind;user
id=sa;password=;";
           conn = genDP.GetConnection(2, connString);
           adapter = genDP.GetDataAdapter(2, connString, sql);
       }
       else if (radioButton3.Checked)
       {
           // Construct your connection string here
           conn = genDP.GetConnection(3, connString);
           adapter = genDP.GetDataAdapter(3, connString, sql);
       }

       try
       {
           conn.Open();
           // Fill a DataSet
           DataSet ds = new DataSet();
           adapter.Fill(ds);
           dataGrid1.DataSource = ds.Tables[0].DefaultView;
       }
       catch (Exception exp)
       {
           MessageBox.Show(exp.Message);
       }
       finally
       {
           conn.Close();
       }
   }

}
W.G. Ryan - MVP - 30 Jan 2006 13:42 GMT
Did you have a question?
> M. Chand is a .NET consultant, author and the admin and founder of C#
> Corner. He has been working with .NET technology since pre beta
[quoted text clipped - 118 lines]
>
> }
Cor Ligthert [MVP] - 30 Jan 2006 14:37 GMT
Anonieke,

Have a look at the factory class

http://msdn2.microsoft.com/en-us/library/system.data.common.dbproviderfactory.aspx

a sample in VB net on our website.

http://www.vb-tips.com/default.aspx?ID=8c3dc2d7-1232-4dd1-817e-22eaaebb2723

I hope this ehlps,

Cor
Otis Mukinfus - 31 Jan 2006 01:04 GMT
Interesting.
What was your question?

I have one.  What do you think will happen in the finally block of the
last method if the connection is not open for some reason?

Did M. Chand write this code?

>M. Chand is a .NET consultant, author and the admin and founder of C#
>Corner. He has been working with .NET technology since pre beta
[quoted text clipped - 118 lines]
>
>}

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.