C# .NET - .csv file export and import in windows forms
Asked By vijaya saladi on 01-Jun-12 01:02 AM
Hai.,
I have a application in that one data grid and 2 buttons are there ,1 button is for import the data,and second one is for export the data from data grid to .csv file.The data grid has 4 columns so when ever the user click the import data, the data in the file will come to the appropriate columns in the data grid.and also when user clicks export the data just stored in the new csv file and asking for name to save the file.So please any one help me for this.
I already developed one function to import the data,it import the data but it completely overwrite the data grid including the columns headers also.so please provide the solution for this.
Thank you.,
Vijaya.S
Jitendra Faye replied to vijaya saladi on 01-Jun-12 01:04 AM

This is code to export CVS file data to database,
try this code-
Here is the working code
string strProvier = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Employees.mdb";
OleDbConnection con = new OleDbConnection(strProvier);
OleDbCommand cmd = new OleDbCommand();
int effectedRow = 0;
int lineCounter = 0;
con.Open();
if ((con.State.ToString() == "Open"))
{
StreamReader stReader = new StreamReader("Employees.csv");
string[] strRowData = null;
while (stReader.Peek() >= 0)
{
lineCounter = lineCounter + 1;
strRowData = stReader.ReadLine().Split(",");
try {
cmd.CommandText = "INSERT INTO tbl_employees(emp_first_name,emp_last_name,emp_salary) VALUES ('" + strRowData(0) + "','" + strRowData(1) + "','" + strRowData(2) + "')";
cmd.Connection = con;
effectedRow = cmd.ExecuteNonQuery();
if ((effectedRow == -1)) {
// Messagebox.Show("Line: " + lineCounter + " Error");
}
else {
// Messagebox.Show("Line: " + lineCounter + " Executed Successfully");
}
}
catch (OleDbException er) {
Messagebox.Show("Line: " + lineCounter + " Error: " + er.Message);
}
}
stReader.Close();
con.Close();
}
else {
Messagebox.Show("Not Connected To Database");
}
This file should contain any separator.
TRy this and let me know.
Somesh Yadav replied to vijaya saladi on 01-Jun-12 01:23 AM
http://doc.openerp.com/v6.0/book/8/8_20_Config/8_20_Config_import_export.html
using export-csv to export to multiple CSV files .NET Framework
Dear all, I am trying to read from a CSV file using import-csv cmdlet and trying to extract relevant information using export-csv it into 3CSV files(exporting to 3 files is mandatory for another process to
How to export contacts to a CSV file Outlook
I have tried many times to export using File -> Import & Export -> Export to a file -> Export to a CSV file -> export from Contacts folder. I always get old outdated contacts
converting CSV comma to csv semi-column .NET Framework
HI ! I am trying to convert a csv file with comma to a csv file with semi-column. I tried import-csv, export-csv, convert from. . . It did not work. I do not understand. I am an idiot
Adding and deleting entries i a CSV file .NET Framework
Hi, I want to add and delete some entries in a CSV file: $file = "c: \ test.csv" $h = import-csv $file # import a csv file $len = $h.count $x = $h[0] # $x is a copy of
In ms project, we will export our ms project into a csv file and send the csv file to server. the code to put ms project content into csv file is: Dim temp, export As String Open GetTempDir & " \ CPLMExport.csv" For Input As #1 While
export CSV file to Access Word
I have a CSV file that I want to export into Access. I Know that I can use an object command in a Word macro Is there any way that I can use a SQL statement to import the whole CSV file? - - Bryan Word VBA Discussions CSV (1) Excel (1) DAO (1) ADO (1) StrTable (1) StrFile
Converting CSV to TAB file .NET Framework
Hi ALL Trying hard to convert csv to tab delimited file using power shell 2.0. Based on what I read this command should work. It of tabbed space for the comma. { $_ -replace ", ", "`t"} Also, not seeing -delimiter option in export-csv which might do the trick as I read somewhere. Thanks in Advance for any suggestions. Sunil Powershell Discussions Error (1) Excel (1) CSV file (1) Encoding (1) Path (1) Larry Weiss (1) SYNTAX (1) Export (1) Try import-csv
how to export csv file with header using bcp? Here's a link that shows how to export to a CSV file using BCP with the header: http: / / weblogs.sqlteam.com / brettk / archive / 2005 / 04 / 13
Export CSV File Without Summary Tasks Project
How do I export a Project file to a CSV file without including all of the summary tasks? I want all of the data I export to have to include date and time fields. Thanks. Project Discussions Outlook (1) Excel (1
[" \ r \ n" new line command in CSV file] Excel
r \ n" new line command in CSV file] Excel [Briefing] I got an Project export Text into the CSV file type. However i face a problem that if my CSV data contain multiple line(eg : " sleep \ r \ nHello \ r \ n"), and open this in the CSV file. We notice that a empty space is appended behind the new line. [Result] sleep<Space