This is a discussion on How to import CSV formatted file using asp.net/c# ? within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi how to import CSV formatted file using asp.net/c# ? Thnx...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| See here you get solutions ![]() |
| |||
| Hi, I came to know about CSV Formatted file in c# can import and saved it in database... private void writeSchema() { try { FileStream fsOutput = new FileStream(txtCSVFolderPath.Text+"\\schema.ini",Fi leMode.Create, FileAccess.Write); StreamWriter srOutput = new StreamWriter (fsOutput); string s1,s2,s3,s4,s5; s1="["+strCSVFile+"]"; s2="ColNameHeader="+bolColName.ToString(); s3="Format="+strFormat; s4="MaxScanRows=25"; s5="CharacterSet=OEM"; srOutput.WriteLine(s1.ToString()+'\n'+s2.ToString( )+'\n'+s3.ToString()+'\n'+s4.ToString()+'\n'+s5.To String()); srOutput.Close (); fsOutput.Close (); } catch(Exception ex) { MessageBox.Show(ex.Message); } finally {} } //Function For Importing Data From CSV File public DataSet ConnectCSV(string filetable) { DataSet ds = new DataSet(); try { // You can get connected to driver either by using DSN or connection string // Create a connection string as below, if you want to use DSN less connection. The DBQ attribute sets the path of directory which contains CSV files string strConnString="Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist Security Info=False"; string sql_select; System.Data.Odbc.OdbcConnection conn; //Create connection to CSV file conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ()); // For creating a connection using DSN, use following line //conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN"); //Open the connection conn.Open(); //Fetch records from CSV sql_select = "select * from ["+ filetable +"]"; obj_oledb_da = new System.Data.Odbc.OdbcDataAdapter(sql_select,conn); //Fill dataset with the records from CSV file obj_oledb_da.Fill(ds,"Stocks"); //Set the datagrid properties dGridCSVdata.DataSource=ds; dGridCSVdata.DataMember="Stocks"; //Close Connection to CSV file conn.Close(); } catch(Exception e) //Error { MessageBox.Show(e.Message); } return ds; } //Button Import CSV Data private void btnImport_Click(object sender, System.EventArgs e) { try { if(txtCSVFolderPath.Text=="") { MessageBox.Show("The Folder Path TextBox cannot be empty.","Warning"); return; } else if(txtCSVFilePath.Text=="") { MessageBox.Show("The File Path TextBox cannot be empty.","Warning"); return; } else if(txtDelimiter.Text=="\"") { MessageBox.Show("You cannot specify (\") as a delimeter.","Warning"); return; } else { int intLengthOfFileName=txtCSVFilePath.Text.Trim().Len gth; int intLastIndex=txtCSVFilePath.Text.Trim().LastIndexO f("\\"); strCSVFile=txtCSVFilePath.Text.Trim().Substring(in tLastIndex,intLengthOfFileName-intLastIndex); strCSVFile=strCSVFile.Remove(0,1).Trim(); Format(); writeSchema(); ConnectCSV(strCSVFile); btnUpload.Enabled=true; } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally {} } It may usefull for u..... Hope u get satisfied..
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily |
| |||
|
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily |
| |||
| private void OnExportGridToCSV(object sender, System.EventArgs e) { // Create the CSV file to which grid data will be exported. StreamWriter sw = new StreamWriter(Server.MapPath("~/GridData.txt"), false); DataTable dt = m_dsProducts.Tables[0]; int iColCount = dt.Columns.Count; for(int i = 0; i < iColCount; i++) { sw.Write(dt.Columns[i]); if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); // Now write all the rows. foreach (DataRow dr in dt.Rows) { for (int i = 0; i < iColCount; i++) { if (!Convert.IsDBNull(dr[i])) { sw.Write(dr[i].ToString()); } if ( i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); } sw.Close(); } |
| |||
| Hi I got error for import csv, that csv included for one empty cell, How can i import csv, pls gimme any idea. thanks
__________________ H2O Without us, no one can survive.. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/asp-asp-net-programming/3186-how-import-csv-formatted-file-using-asp-net-c.html | |||
| Posted By | For | Type | Date |
| import csv asp.net - Dot Net Search Engine swicki - powered by eurekster | This thread | Refback | 01-14-2008 12:37 AM |
| datatable to csv in C# - Dot Net Search Engine swicki - powered by eurekster | This thread | Refback | 01-09-2008 01:59 PM |
| datatable to csv in C# - Dot Net Search Engine swicki - powered by eurekster | This thread | Refback | 12-12-2007 07:27 AM |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| import http CSV or TXT to database ??? | muller | Database Support | 0 | 03-25-2008 04:50 AM |
| File extension of the code file & object repository file in QTP | vigneshgets | Testing Tools | 1 | 01-16-2008 11:43 PM |
| How do we import or export data in MySql | Jeyaseelansarc | Database Support | 8 | 08-17-2007 07:25 AM |
| How can we import and export using BCP utility in SQL? | oxygen | Database Support | 1 | 07-26-2007 04:33 AM |
| How to import MySQL Dump files? | kingmaker | Database Support | 2 | 07-24-2007 04:47 AM |