DataHelper.ExecuteDataset for different database providers

I have a small utility .Net that was used to transform data from one database to another. Initially it was written using SQLHelper DAAB class. Then it was extended to work with OledbHelper , that was created based on SqlHelper. Now it was requested to work with ODBC provider as well.
I know, that I can use Enterprise library DAAB(e.g see here) , but I decided not  involve additional DLLs and extra settings in config file for this simple application.

The static finction to return dataset for specified SQL and nominated provider is below:

        public const string OleDBProviderInvariantName="System.Data.OleDb";

        public const string OdbcProviderInvariantName="System.Data.Odbc";

        public const string SQLProviderInvariantName = "System.Data.SqlClient";

  

     //COnsider to use DAAB in Enterprize Library instead

        //Provider name is case sensitive, such as "System.Data.OleDb","System.Data.Odbc","System.Data.SqlClient";

        public static DataSet ExecuteDataset(string ProviderInvariantName, string sConnString, string sSQL)

        {

            DebugHelper.TracedLine("sConnString=" + sConnString);

            DbProviderFactory fact = DbProviderFactories.GetFactory(ProviderInvariantName);

            DbConnection conn = fact.CreateConnection();

            conn.ConnectionString = sConnString;

             DebugHelper.TracedLine("(sSQL=" + sSQL);

            DbCommand cmdSelect = conn.CreateCommand();

            cmdSelect.CommandText = sSQL;

            DbDataAdapter da = fact.CreateDataAdapter();

            da.SelectCommand = cmdSelect;

            DataSet ds = new DataSet();

            try

            {

                da.Fill(ds);//Table

            }

            catch (Exception exc) //MNF 18/5/2005

            {

                throw new FSCSharpLib.ExceptionWithDetails(exc.Message, " sSQL = " + sSQL, exc);

            }

            return ds;

        }



 

posted @ Monday, March 19, 2007 10:57 AM

Print

Comments on this entry:

# re: DataHelper.ExecuteDataset for different database providers

Left by Michael Freidgeim at 3/27/2007 11:09 PM
Gravatar
towels,
The function can be used just to simplify code to retrieve Dataset from particular data provider. To save dataset to another database you need function like UpdateDataSet from Enterprise Library Data Access Application Block.Example of use is described at http://msdn.microsoft.com/msdnmag/issues/05/10/DataPoints/default.aspx#S4

Your comment:



 (will not be displayed)


 
 
 
Please add 6 and 3 and type the answer here:
 

Live Comment Preview:

 
«September»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011