|
a CSV file is just plain text (open one in Notepad to see), so all you need to do is read the text file, and dump out the lines to new files
pseudocode:
OPEN input.csv
OPEN output1.csv
OPEN output2.csv
header = input.GETLINE
output1.WRITELINE(header)
output2.WRITELINE(header)
line = 0;
WHILE ( line < 1000 & !input.EOF )
row = input.GETLINE
output1.WRITELINE(row)
line++
ENDWHILE
output1.CLOSE
WHILE( !input.EOF )
row = input.getline
output2.WRITELINE(row)
ENDWHILE
output2.CLOSE
input.CLOSE
|
|
|
|
|
There is one exception: CSV doesnt say ANYTHING about whats in there. So, there actually might be headers in the first line of the file.
If thats the case, you will want to read the first line seperately, store it, and write it to ALL output files before starting to distribute the other entries.
Cheers,
Sebastian
--
Contra vim mortem non est medicamen in hortem.
|
|
|
|
|
Thanks for your help benjymous.
I have tried working through this, but no success yet; I know it is a basic thing I am missing, but not sure what. The code I have so far is:
using System;
using System.Collections;
using System.IO;
using System.Text;
public class CSVReader
{
// Open the files to write to
string fileName = @"C:\data\Test Data\Test\CIS_KASN.txt";
string textLine;
int row = 1;
}
public CSVReader(string fileName)
{
StreamReader fileReader = File.OpenText(fileName);
StreamWriter fileWriter1 = File.CreateText(@"C:data\Test Data\Test\Output1 CIS_KASN.txt");
StreamWriter fileWriter2 = File.CreateText(@"C:data\Test Data\Test\Output2 CIS_KASN.txt");
while ((textLine = fileReader.ReadLine()) != null && row <= 1000)
{
// Write to output file
fileWriter1.WriteLine(textLine);
row++;
}
fileWriter1.Close();
while ((textLine = fileReader.ReadLine()) != null & row > 1000)
{
// Write to output file
fileWriter2.WriteLine(textLine);
}
fileWriter2.Close();
}
Does this make much sense?
Also, point taken about the header; I'll work that part out when I get the main part working!
Thanks all.
|
|
|
|
|
I'm just curious what is the root cause of having to break up the CSV?
|
|
|
|
|
Another programme has been written that only allows CSV files of 1000 records per go. I don't know what it's been written in etc, but suffice to say it will take a long time for that side of things to be changed; this is seen as quicker. So any help is much appreciated!
|
|
|
|
|
Salam
how can i save uft-8 format characters to database
is there is any method or technique that convert uft-8 to normal and then reencode it.
in c#
sikandar
|
|
|
|
|
C# ha a number of classes for converting between text encodings. Is UFT-8 the same as ASCII ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
if i save نام
this world to data base when i retrieve i get ?????
how can i get the origninal
sikandar
|
|
|
|
|
UTF-8 is an encoding, and you don't save encoded text to the database.
Use a unicode data type. In SQL Server, for example, the unicode data types are named nvarchar and ntext .
---
b { font-weight: normal; }
|
|
|
|
|
|
lo,
i wish to delete a selected row,
i could do this by using next code:
//get the selected row
DataRow selectedrow = db1DataSet.tabel1.Rows[dataGridView1.CurrentRow.Index ];
// delete the row in datagrid
selectedrow.Delete();
// apply delete to the database
try
{
tabel1TableAdapter.Update(db1DataSet.tabel1); // here i get an error: not a valid delete command
db1DataSet.AcceptChanges();
this.tabel1TableAdapter.Fill(this.db1DataSet.tabel1);
Application.DoEvents();
}
catch (OleDbException ex)
{
db1DataSet.RejectChanges();
lbl_error.text =ex.Message;
}
does anybody know how i should solve this?
-- modified at 6:03 Friday 15th September, 2006
|
|
|
|
|
i could do this by using next code:
DataRow selectedrow = b1DataSet.tabel1.Rows[ (i 'm not sure of this )dataGridView1.selectedRows.index ];
No, you couldn't.
There is a small problem. You're trying to get a index of an array itself. Thats not possible because the array itself has no index. It consists of several elements which are identified by an index.
Try it this way:
<br />
DataRow selectedrow = b1DataSet.tabel1.Rows[dataGridView1.SelectedRows[0].Index];<br />
If you are able to select multiple rows at the same time only ONE of those rows (the "first" selected row) will be assigned to selectedrow.
If you want to get all rows you just will have to add some loops
|
|
|
|
|
datagrid.CurrentRowIndex gives the currently selected row.
|
|
|
|
|
This looks like you didn't set up your data adapter properly.
I have done some stuff with data adapters some time ago and i remember
it was a lot of work to get the update method work correctly.
if you are not familiar with data adapters i would suggest you to read
some tutorials first.
I just googled a bit and found something which could avoid the whole setup... its called CommandBuilder. I haven't heard of before but read this page to set up your data adapter properly
http://msdn2.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx
|
|
|
|
|
thx,
still left with a prob
see modified
|
|
|
|
|
i have used the update before in this project and i dont have errors there,
i thought that if my adapter wasn't set up properly the update wouldn't work anywere.
i use it to add a row, and this works perfect
|
|
|
|
|
what type is tabel1TableAdapter (I have not done database access this way before so i am stepping on new ground).
Is the row in the table related to any other tables which is causing some type of constrain error?
|
|
|
|
|
adding a row does not mean that deleting and modifying work as well. have a read for the setup. it also would be nice to know which kind of data adapter you are using.
there are several types that's why posting the line where the tableadapter is defined would be nice.
|
|
|
|
|
no, the adapter is linkt to only 1 table,
i get the error here, after i deleted it form the datagrid with the delet() funtion:
// apply delete to the database
try
{
tabel1TableAdapter.Update(db1DataSet.tabel1);//here i get the error
db1DataSet.AcceptChanges();
// refresh after the delete
this.tabel1TableAdapter.Fill(this.db1DataSet.tabel1);
Application.DoEvents();
}
catch (System.Exception ex)
{
db1DataSet.RejectChanges();
lbl_error.Text= ex.Message;
}
if i switch tabel1..... and db1dataSet from place, i dont get it but then it doesnt change in the database
thx
|
|
|
|
|
tabel1TableAdapter.Connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\user\Desktop\test\prog\database2005\access\" + i + ".mdb";
and string i = "day" + DateTime.Now.DayOfYear.ToString();
|
|
|
|
|
okay, this is still not an answer to my question but i assume you're using a OleDbDataAdapter... use the link i posted some comments before - if it doesn't work for you write down the problems you're expiriencing.
good luck,
mik
|
|
|
|
|
sorry,
i'm not using oledbdataadapter, i used the wizard by using the data in the toolbar
i know now that i need to make a deletecommand but i'm not sure how,
?=tabel1TableAdapter.Connection.CreateCommand();
what do i use here to get a deletecommand?
i cant find it anywere
thx for the help
|
|
|
|
|
i used the update to add a row as wel and here it works perfect
so i dont think its caused by my settings of the dataadapter
|
|
|
|
|
That simply means that your dataAdapter is succesfully linking to the database. Your problem lies with the delete command inside the Update function.
Did you write these yourself or did you use the IDE (VS2003 etc)?
Can you also embed the actual error result into the message as they are usually quite explicit. You will need to higlight the error message text with your mouse and then use Ctrl-C and Ctrl-V.
Glen Harvy
|
|
|
|
|
i used the wizard apply in from the toolbar datagrid vs2005
so how do i make a deletecommand than?
i know how to do it with oledb but i dont use that,
i foud the way to create command but not to specify it as a delete command
?=tabel1TableAdapter.Connection.CreateCommand();
do i use the dbDataSet of what?
i am realy confused
and were do i write this? together with the connectionstring?
this is the error i am getting now :
Update requires a valid DeleteCommand when passed DataRow collection with deleted rows.
thx
|
|
|
|