How to do a mapping from an Excel column to a database column

0

I needed help to do the specific mapping of a column from an excel file to a column in a database. I can read the excel file but I just can not map the file to the column I want in the database. If you need any part of the code just ask like any other question.

I wanted to do something like this:

    
asked by anonymous 10.10.2017 / 13:22

2 answers

1

For your information, just create the tables in Bd and perform CRUD (Create, Read, Update, Delete) by the application.

Example: Since you have not specified which database you are using I will use Postgres, but it should work for everyone.

First install an ADO.NET data provider in the project, it can be done by the Nuget Manager. I installed Npgsql.

Create the database to receive this data.

CREATE TABLE public."exemplodb"
(
  browse character varying NOT NULL,
  colunadadb1 character varying,
  colunadadb2 character varying,
  colunadadb3 character varying,
  colunadadb4 character varying,
  CONSTRAINT "PK" PRIMARY KEY (browse)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public."exemplodb"
  OWNER TO postgres;

Now when you click the Send BD button you program your data manipulation.

        private void enviardbButton_Click(object sender, EventArgs e)
        {
            using (NpgsqlConnection conn = new NpgsqlConnection("sua connection string aqui"))
            {
                try
                {

                    string commandText = 
                        $"insert into exemplosdb " +
                        $"(browse, colunadadb1, colunadadb2, colunadadb3, colunadadb4) " +
                        $"values ('{browseComboBox.Text}' '{colunadb1ComboBox.Text}', " +
                        $"'{colunadb1ComboBox.Text}', '{colunadb1ComboBox.Text}', '{colunadb1ComboBox.Text}');";
                    int linhasAfetadas = 0;
                    using (NpgsqlCommand command = new NpgsqlCommand(commandText, conn))
                    {
                        conn.Open();
                        linhasAfetadas = command.ExecuteNonQuery();
                        conn.Close();
                    }
                    if (linhasAfetadas == 1)
                    {
                        //Dados da tela gravados com sucesso.
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            }
        }

Making it clear that this is a super simplified example just to demonstrate! Several important concepts, such as separation of responsibilities, layered programming, among others, were ignored for simplicity, but they are very important.

    
10.10.2017 / 14:04
0

I believe the easiest way for this case is to use the SQL Server tool and import data (Sql Server Import and Export Data) and create a new table in the database temporarily, so that it is possible to do an insert of new data from a select or update from a join, to load the desired column.

    
10.10.2017 / 15:09