Click here to Skip to main content
Licence 
First Posted 4 Sep 2007
Views 7,805
Downloads 31
Bookmarked 12 times

SqlDbAdaptor hides underlying database

By | 4 Sep 2007 | Article
SqlDbAdaptor hides underlying database and makes very easy to switch database without a single line of code change

Introduction

The SqlAdaptor hides underlying database so it is very easy to switch databases if necessary without writing a code


Using the code

            string conection_string;
            SqlAdaptor sql_adaptor = new SqlAdaptor(DatabaseType.SqlServer);
            sql_adaptor.connect(conection_string);
            //ExecuteNonQuery
            sql_adaptor.ExecuteNonQuery("insert into tblTest ( Name,Age ) VALUES ('Joe',28)");
            //ExecuteReader
            IDataReader reader = sql_adaptor.ExecuteReader("select * from tblTest");
            //ExecuteScalar
            int count = sql_adaptor.ExecuteScalar("select count(*) from tblTest where Age = 28");

            //more advanced usage
            string query = "select * from tblTest";
            using (IDbCommand cmd = sql_adaptor.Connection.CreateCommand())
            {
                cmd.CommandText = query;
                IDataReader reader = cmd.ExecuteReader();
            }
            //use transactions
            string query = "insert into tblTest ( Name,Age ) VALUES ('Joe',28)";
            using (IDbTransaction transaction = sql_adaptor.Connection.BeginTransaction())
            {
                using (IDbCommand cmd = sql_adaptor.Connection.CreateCommand())
                {
                    cmd.CommandText = query;
                    cmd.ExecuteNonQuery();
                }
                transaction.Commit();
            }
            //using parameters
            ArrayList par_list = new ArrayList();
            IDataParameter par = sql_adaptor.DBFactory.CreateParameter();
            par.DbType = DbType.String;
            par.Direction = ParameterDirection.Input;
            par.ParameterName = "@Name";
            par.SourceColumn = "Name";
            par_list.Add(par);
            IDataReader reader =  sql_adaptor.ExecuteReader("select * from tblTest where Name = @Name", par_list);

Points of Interest

I am practicing combat martial arts; I am software developer and also manage a www.handymandy.ca web site

History


License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Igor Sokolsky

Software Developer (Senior)

Canada Canada

Member



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
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 4 Sep 2007
Article Copyright 2007 by Igor Sokolsky
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid