I have a .csv file that I am reading into C# via an OLEDBConnection. I am loading this into a DataSet. This step works fine. My data in the .csv file does not have any heading data/column headings. I want to add column headings to my DataSet in C# rather than directly into the .csv file. If I try to add column headings, then it just creates a new column on the end of the data, however I want to add the heading to the existing columns that already have data in it rather than create a new column.
Just access the column using index and the change the name
DataTable table = your datatable;
table.Columns[0].ColumnName = "Foo";
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.
I need to replace table created by OleDbCommand in Excel file. The new table has the same name, but could has different structure. I managed this by deleting sheet via Interop.Excel but there is problem when other sheets refer to this sheet. All formulas are lost. I noticed that creating new table via OleDbCommand creates new Defined name in Names property (Names manager). I've tried to create new table with other name, copy table headers and change name location in:
workbook.Names.Item(ShName, Type.Missing, Type.Missing).RefersTo=oldSheetRange
Where oldSheetRange is column headers range. Unfortunately when I try to insert new data rows they do not insert to the table (no error occurs).
Is there any option to replace table saving reffering formulas?
Regards, Kuba
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.
I have a weird problem on my hand. I am trying to read an oracle database and using some queries to get info back. I have a datagridview with column names already set at the beginning of the program. Here is an example setup of my datagridview columns
Number Word Sentence Paragraph
Now i am reading a database and selecting its columns named N S P representing Number Sentence and Paragraph respectively. How can i load the results of the query into a datatable and display the contents under my datagridview so the N contents in the database are displayed under the Number column and nothing under word column etc. I can always query by selecting N, S and P one at a time, but i want to load all the data at once.
THanks
You can change the column names in the SQL by writing
SELECT N AS Number, S AS Sentence, ...