Distinct values in an excel data source - c#

I have a source excel file where my "key" is held in column B. I want to add rows that contain different distinct key values to different tables in a database, using a list of datatables to allow for the number of distinct keys to be variable.
Is it better practise to select the distinct records straight from the sheet itself, or to pull the entire sheet into a "master" datatable and query the datatable for my separate values?

It would be better if you first use distinct and then filter records, it will improve speed and performance.

Related

Fastest way to get the distinction/delta between a local table and the values of a database table

I have the following requirements:
I have many different database tables for example a table which shows different materialnumbers. There are many thousands rows in the database table (>100.000)
A user could import data with an excel file with thousends of rows.
The distinction/ difference between the excel file and the database-table should be added. The allready existing materialnumbers should be updated.
Is there a faster way than this to get the allready existing values in entity framework:
var allreadyExistingMaterialnumbersInDatabase = myDbContext.Materialnumbers.Where(x=>myExcelListMaterialnumbers.Contains(x.Materialnumber)).ToList();
After that step i have to do a update for all the data which is returned in the code example and then i have to insert all the difference between the list allreadyExistingMaterialNumbersInDatabase and myExcelListMaterialnumbers.
Thanks for your help.

DataTable expressions referencing to two different tables

I have two hidden Datatables, one of which gets its data from a database, and the other from a static .csv file. I want to create a DataGridView with columns that has custom datatable expressions (just like a spreadsheet).
My question is: how do I formulate expressions in the datagridview to search for data in the other two tables?
Example: Table B has references for each product, a static unique id. Table A also have a reference to each product but also includes status for that product.
In the DataGridView I have three columns (ID, PRODUCT, STATUS). How can I write the expressions to fetch data from the other tables?
This is just how I think this can be solved, if you have another idea please let me know.
Thank you!
using Merge two tables
1- convert table1 to dataTable dtOne.
2- convert csv data to dataTable dtTwo.
3- dtAll = new Datatable();
dtAll = dtOne.Copy();
dtAll.Merge(dtTwo);
dataGridView.DataSource=dtAll;

Split t-sql results into data tables, by non-numerical value in a field

I writing a C# program to output t-SQL records into separate tabs in an excel spreadsheet, split by the person the records belong to.
I have seen that I can have many data tables in a single data set, and turn each into a separate tab (how to store multiple DataTables into single DataSet in c#?), so now I need to populate my data tables.
I do not have a fixed list of people, it will vary each time the program is run, and a person could have any number of records assigned to them.
Is there a way of doing this using SQL / C# using something like order or group by; or do I have to get my results, pick up the list of people, then loop each SQL query for that specific person and feed that into a new data table?
Thought I'd ask if anyone knew a short way before I did it the long way, because this can't be an uncommon thing to do; so I suspect there must be a simpler way.
Normally you get one DataTable per SELECT statement.
However, you could just select everything and then use LINQ to group the data and fill your DataTables. See if this is any help.
It depends on the table structure, as well as for the source as for the destination.
If you have multiple source tables you can append them together with the UNION statement. Which gives the distinct value of all tables. You can use UNION ALL to keep duplicate values.
SELECT customer_key, customer_name, customer_address
FROM table_1
WHERE customer_key = #Customer_key
UNION (or UNION ALL)
SELECT customer_key, customer_name, customer_address
FROM table_2
WHERE Customer_key = #Customer_key
UNION etc..

SQL batch insert, avoid duplicates, no PK

I was given a task to insert over 1000 rows with 4 columns. The table in question does not have a PK or FK. Let's say it contains columns ID, CustomerNo, Description. The records needed to be inserted can have the same CustomerNo and Description values.
I read about importing data to a temporary table, comparing it with the real table, removing duplicates, and moving new records to the real table.
I also could have 1000 queries that check if such a record already exists and insert data if it does not. But I'm too ashamed to try that out for obvious reasons.
I'm not expecting any specific code, because I did not give any specific details. What I'm hoping for is some pseudocode or general advice for completing such tasks. I can't wait to give some upvotes!
So the idea is, you don't want to insert an entry if there's already an entry with the same ID?
If so, after you import your data into a temporary table, you can accomplish what you're looking for in the where clause of a select statement:
insert into table
select ID, CustomerNo, Description from #data_source
where (#data_source.ID not in (select table.ID from table))
I would suggest to you to load the data into a temp table or variable table. Then you can do a "Select Into" using the distinct key word which will removed the duplicated records.
you will always need to read the target table, unless you bulk load the target table into a temp table(in this point you will have two temp tables) compare both, eliminate duplicates and then insert in target table, but even this is not accurate, because you can have a new insert in the target table while you do this.

Insert rows into Access Table in order from c#

I have a sorted list of insert statements that I am trying to write to an Access db. I have triple verified that the list of insert statements is in the correct order. When I open the mdb file the records are never in order. Maybe for the first 100 records, but after that it starts getting out of whack.
I am really at a loss here, any ideas? Note that this table is being created in C# first dynamically - i.e. the set of of columns is not predictable each time this code needs to be run.
Maybe you just need to add an ID field to the tables and then the insertion order should be maintained.
When adding rows to any database, the concept of "Order inside a Table" is meaningless.
You get your order when retrieving records by using an ORDER BY.
Make sure you have an ID or TimeStamp column to sort on.

Categories

Resources