I have a bot that will run daily, after the execution, the bot will transfer the data from excel to SQL. How can I make sure that what the robot isn’t importing data that it exists already. In other words: If the robot fails during transferring the data, what was transferred, will stay on SQL, then i will need to fix the bot, and rerun it then, How can I make the bot to import the data that wasn’t transferred only. I have an idea but don’t know how to make it happen: Check if the rows exists.
Here is how i’m importing the data to SQL:
1- Connect to SQL
2- Read Range
3- Insert to SQL
4- Disconnect from database
What you can do is,
before inserting the data into SQL; Retrieve the data from Sql
use dt3= dt2.AsEnumerable().Except(dt1.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable
to find the rows which aren’t present in sql already.
Then just insert the records present in dt3.
dt3 = is a new variable type of DataTable
dt2 = Is the datatable that we got from retrieving the data from SQL
dt1 = is the DataTable that I want to insert to SQL
It is still not working, the issue is: it is still inserting the data.
I’m testing your idea with SQL data table and excel data table that contains same data but it is still inserting the data into it.
Here is what I’m doing exactly:
1- Connect to Database(SQL)
2- Read Range ( Excel file that I want get the data from it ) to dtResults var
3- Run Query “Select * from table name;”
4- Assign dt_Compared = SQL_DT.AsEnumerable().Except(dtResults.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable
5- Insert to database (SQL), DataTable that i’m inserting is the dt_Copmared.
But still inserting the data even though it is same data, which means my data is duplicated on SQL. any idea?
Still adding the same info to SQL, I mean it is duplicating the info. and during debugging, i have notice that the data table in dt_Compared, is the same one in Excel, that means it is not comparing the data tables between Excel sheet and SQL.