Thursday, September 8, 2011

Registering data provider at runtime

In .Net, you may either register new data provider in the app.config file or register the data provider programmatically.
static bool _has_reg_sqlite_provider = false;

        public static void RegisterSqliteProvider()
        {
            if (_has_reg_sqlite_provider)
            {
                return;
            }

            var dataSet = System.Configuration.ConfigurationManager.GetSection("system.data") as System.Data.DataSet;

            if ((dataSet != null)
                && (dataSet.Tables.Count > 0))
            {
                DataRow[] rows = dataSet.Tables[0].Select("InvariantName='System.Data.SQLite'");

                if ((rows == null)
                    || (rows.Length == 0))
                {
                    dataSet.Tables[0].Rows.Add("SQLite Data Provider",
                        "SQLite Data Provider",
                        "System.Data.SQLite",
                        "System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.74.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"
                    );

                    _has_reg_sqlite_provider = true;
                }
            }
        }

No comments:

Post a Comment