using a datatable or just load data infile? - c#

i have a csv file which data is in this type...(date,time,id)
20120131, 07:17:40, BK01
20120131, 07:17:41, BK02
20120121, 07:50:04, BK05
then..i want to store this data into a table..
i read about LOAD DATA INFILE but...
as far as i know(not much), LDI
is for when the data column in my csv is totally same with my db table..
but in my db table there is another column..
which is Status..
20120131, 07:17:40, BK01, STATUSHERE
20120131, 07:17:41, BK02, ??
20120121, 07:50:04, BK05, ??
this Status based on the time in csv file...
i mean here..how can i set the status and then store it
with the data from csv file altogether into?
or is it by using data table/dataset?
thank you in advance..

LOAD DATA INFILE does not require the columns in CSV to match the columns in your table. You can specify column names in your LOAD DATA INFILE query in which you would like to insert values. Here is a text snippet from MySQL documentation
The following example loads all columns of the persondata table:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata;
By default, when no column list is provided at the end of the LOAD DATA INFILE
statement, input lines are expected to contain a field for each table
column. If you want to load only some of a table's columns, specify a
column list:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata
(col1,col2,...);
You must also specify a column list if the order of
the fields in the input file differs from the order of the columns in
the table. Otherwise, MySQL cannot tell how to match input fields with
table columns.
However, I'm not sure I fully understand your need -
NEED 1: would you like to insert only the 3 columns in your CSV file to your table ignoring what goes into the Status column? If yes, the above can help.
NEED 2: would you like to insert values into Status column? In that case, simply add a fourth column to your CSV file and then run LOAD DATA INFILE.
NEED 3: would you like to calculate the values for Status column at run-time? Use SET syntax in this case. The below text snippet might help:
The column list can contain either column names or user variables.
With user variables, the SET clause enables you to perform
transformations on their values before assigning the result to
columns.
User variables in the SET clause can be used in several ways. The
following example uses the first input column directly for the value
of t1.column1, and assigns the second input column to a user variable
that is subjected to a division operation before being used for the
value of t1.column2:
LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, #var1) SET
column2 = #var1/100;
The SET clause can be used to supply values not derived from the input file. The following statement sets column3 to the current date and time:
LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, column2) SET
column3 = CURRENT_TIMESTAMP;
You can also discard an input value by
assigning it to a user variable and not assigning the variable to a
table column:
LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, #dummy,
column2, #dummy, column3);
Hope it helps! You may read more on the documentation page.

Related

bulk compare in sql

I have a csv file with one column. Each row in this column may have a match record in TestTable from database (sample table only). I would like to send the entire data via C# to the stored procedure one time and return the rows from the database if it has a match.
Input parameter (csv column):
Stringcolumn
apple
banana
copper
dig
....
Output (possibly dataset):
ID|StringColumn|AddedDate
2|apple|2021-01-02
35|copper|2021-01-02
The requirement is to send and receive the data via C#.
Right now, I'm thinking to use User-Defined Table Types in MS SQL to receive the data.
Thank you.
If its a big csv I would recommend dumping it in a temp table and then using a join to get the data. Alternatively you can use user defined table type to pass the data into a stored procedure

Keeping empty column names as such while reading the excel sheet data

I am reading an excel file and saving contents to database.
I have two columns , first column name is blank and second column name is A.
Both columns has rows under it .
When I am executing "select * from ["+excel[i]+"]") using oledbconnection,
empty column is automatically replaced with F1. I need both the columns and data as it is available from excel sheet.
How to avoid it?
I don't think there is a way to avoid this. This is a normal behaviour of OleDB when connected to Excel, if it finds empty columns, it will either take the first row as column name or generate some default names as of F1,F2,F3...etc

how Sql bulk copy works?

I want to use SqlBulkCopy C# to insert rows to Sql DataBase
but I don't understand if I define a table in the Db with X columns and I want to insert 2 DataTables- one of them contains only y/x columns and the second contains only z/x columns.
when I use SqlBulkCopy, does it auto check the column name and insert value only if it is the appropriate column and null if not, or it always inserts values in the first columns and the last columns will be empty?
I tried to search it but I haven't find it?
can anyone help?
Look for :SqlBulkCopyColumnMapping
When SqlBulkCopyColumnMapping is used, only columns for which mappings are created will be copied.
If you do not create a mapping for a column, it will be ignored by the copy process.
Take a look at the SqlBulkCopy.ColumnMappings property. It allows you to map source and destination columns when the column count or positions do not match.

C# PISDK - How do you pull a full column of data from a PISDK server using C#?

I am looking to pull full column data from a table in a PISDK Server, and then populate any blank fields in that column with the field from another column in the same row. This would be similar to a select statement in SQL:
Select nvl(column1, column2)
From table1
if you are not familiar with SQL, this just returns column2 if column1 is null, otherwise it will return column1
My main problems are that I can get a point from the server, but I cannot get a full column and that I am not sure how to replace null column1 fields with column2.
I was able to answer my own question. For future reference, to do this load in the PI tags as a column in a csv and then copy each row into the correct column1 of your other csv. then simply find anywhere the tag is null and pull its corresponding column2 value.

Inserting unique id for file uploaded to database using SqlBulkCopy

There is a table with the following columns:
ProdID
Specification
Value
I've got a csv file with a schema that only has the last two columns - Specification and Value. In other words they are my csv headers. Now I need to associate the values of this file against one unique id.
The significance of that id is that it is a reference to the document; its not important here, but, for now assume it is reqired by the system.
Now while I use the SqlBulkCopy class to insert is there a way that I can still insert some value for ProdID such that each row inserted will have the same ProdID for that file I am trying to bulk copy...?
That depends. if ProdID is an identity field in the database, the database will fill it for you when you perform the bulk copy.
If it's not, you will need to fill it yourself and pass the three columns to SqlBulkCopy, instead of just the two.
You'd have to do something on the lines of the following code:
string cmdTxt = "SELECT " + ProdID + ", * FROM [sample.csv]";
It doesn't matter where you place the ProdID field in the select clause above; in the beginning or end, you'll get the result set with ProdID as the first field...

Categories