How to insert values in Database Table using UiPath?

Hello friends.
I’m having some difficulty inserting data into a table in my database.
I would like that at each iteration of my For each Row in datatable loop the current row is inserted into the database table.
So I can connect this table with Power BI and display the data in a dashboard.
Does anyone have any suggestions on how I can do this or solve my problem?

@Matheus_Antunes

Please remove the current expression and click on plus icon on the query option…then select open advanced editor and use a sbelow

"Insret Into ……. values(" + CelularCliente + "," + nomeCliente + ")"

Cheers

Now my script return this error:

image

Look my CSV File

image

@Matheus_Antunes

I guess values are to be enclosed in single quotes when its a string just now added them use "Insret Into ……. values(" + CelularCliente + ",'" + nomeCliente + "')"

Also if there are any single quotes in the data you are providing then use nomeCliente.Replace("''","'") this is to escape single quotes

cheers

Thanks so much!!!
I have one last question.
I have to put in my database the status of the client.
For example
If the message is sent, something like “Success” or “Unsuccessful” must be inserted in the CSV, but this column does not exist in my CSV, it should be filled in according to the loop for each row in datatable.
How can I do this?
I thought of saving in a variable “Status” the value of “Success” or “Unsuccessful” depending on the result of an IF condition.
My question is, how can I insert this variable inside the CSV?

@Matheus_Antunes

You would have read the csv into datatable …now use add data column activity and add a new column for status

Inside loop use Assign activity with currentrow("Status") = "Successful or Not"

After the loop use write csv and write the data back

hope this helps

cheers

Is it possible for me to insert the value of a variable directly into a database column?
Without this variable being in the CSV.

Get rid of the For Each Row in Datatable and just use the Insert activity.

https://docs.uipath.com/activities/other/latest/user-guide/insert-data-table

@Matheus_Antunes

Yes you can directly add…just in the insert row …use successful or unsuccessful as need or assign to variable using if confition and add the variable in the values again

Cheers

In my case I need to insert the current line every iteration that my for each goes through.
But I would like to add a status value directly to the database.
This status does not exist in the datatable.
It would be added depending on a condition IF True = “Sucess” Else = “Not Success”

Why?

So add the column, then populate it in the For Each Row in Datatable.

Then after the For Each Row, use the Insert activity.

How can I send the current moment to a table in my database?
I created a variable of type System.DateTime and inside the Assign activity I assigned the Now function to it.
But inside the Insert UiPath returns me Option Strict On dont allow implicit conversions of “Date” to “String”
data_de_envio is the variable of DateTime

The SQL script:

“INSERT INTO ClientesCobranca (celular, nome_cliente, inscricao_cliente, status, data_de_envio)
values (” + Celular + “,'” + NomeSemAspas + “‘,’” + Inscricao + "', ‘Número inválido’, "+ data_de_envio + “)”

@Matheus_Antunes

Please use like this …and format should match with what you need in sql

data_de_envio.ToString("MM/dd/yyyy hh:mm:ss tt")

Change fate format as needed

Cheers