/// <summary> /// Reads configuration data into a new instance from SQL Server /// that is returned. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public override T Read <T>() { using (SqlDataAccess data = new SqlDataAccess(ConnectionString, ProviderName)) { string sql = "select * from [" + Tablename + "] where id=" + Key.ToString(); DbDataReader reader = null; try { DbCommand command = data.CreateCommand(sql); if (command == null) { SetError(data.ErrorMessage); return(null); } reader = command.ExecuteReader(); if (reader == null) { SetError(data.ErrorMessage); return(null); } } catch (SqlException ex) { if (ex.Number == 208) { sql = @"CREATE TABLE [" + Tablename + @"] ( [id] [int] , [ConfigData] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS)"; try { data.ExecuteNonQuery(sql); } catch { return(null); } // try again if we were able to create the table return(Read <T>()); } } catch (DbException dbEx) { // SQL CE Table doesn't exist if (dbEx.ErrorCode == -2147467259) { sql = String.Format( @"CREATE TABLE [{0}] ( [id] [int] , [ConfigData] [ntext] )", Tablename); try { data.ExecuteNonQuery(sql); } catch { return(null); } // try again if we were able to create the table var inst = Read <T>(); // if we got it write it to the db Write(inst); return(inst); } return(null); } catch (Exception ex) { this.SetError(ex); if (reader != null) { reader.Close(); } data.CloseConnection(); return(null); } string xmlConfig = null; if (reader.Read()) { xmlConfig = (string)reader["ConfigData"]; } reader.Close(); data.CloseConnection(); if (string.IsNullOrEmpty(xmlConfig)) { T newInstance = new T(); newInstance.Provider = this; return(newInstance); } T instance = Read <T>(xmlConfig); return(instance); } }