Adf.Data.DatabaseQueryHandler.New C# (CSharp) Method

New() public method

Create a new data row of IInternalState to insert new data into database.
public New ( IAdfQuery query ) : IInternalState
query IAdfQuery The that defines the datasource name and query statement.
return IInternalState
        public IInternalState New(IAdfQuery query)
        {
            if (query == null) return NullInternalState.Null;

            var result = new DataSet { EnforceConstraints = false };

            IDbConnection connection = Provider.GetConnection(DataSource);
            IDbDataAdapter da = Provider.GetAdapter();

            try
            {
                if (connection.State == ConnectionState.Closed) connection.Open();

                da.TableMappings.Add("Table", query.LeadTable());

                IDbCommand command = Provider.GetCommand(DataSource, connection, query);
                da.SelectCommand = command;

                result.Load(command.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly), LoadOption.OverwriteChanges, query.LeadTable());
                result.Tables[0].Rows.Add(result.Tables[0].NewRow());
            }
            catch (Exception exception)
            {
                Provider.HandleException(exception, DataSource, query);
            }
            finally
            {
                if (connection.State == ConnectionState.Open) connection.Close();
            }

            return IsResultEmpty(result) ? NullInternalState.Null : RowState.New(result.Tables[0].Rows[0]);
        }