How to read a CSV file in C #

Source: Internet
Author: User
Tags readline

How to read a CSV file in C #

Use a System.Web.UI.HtmlControls.HtmlInputFile to handle file selection.

The following is the code in the button click event, which is used to read the contents of the file when the file is selected.
<%@ Page language= "C #"%>
<%@ import namespace= "System.Data"%>
<%@ import namespace= "System.Data.OleDb"%>
<script runat= "Server" >

void Page_Load (object sender, EventArgs e) {

String ConnectionString = @ "Provider=Microsoft.Jet.OLEDB.4.0; Data source=c:banddatabase; Extended properties= "" text; Hdr=yes; ""; ";
String commandtext = "Select * from Csv.txt";

OleDbConnection myconnection = new OleDbConnection (ConnectionString);
OleDbCommand mycommand = new OleDbCommand (commandtext, MyConnection);

Myconnection.open ();

DataGrid1.DataSource = Mycommand.executereader (commandbehavior.closeconnection);
Datagrid1.databind ();

Myconnection.close ();
}

</script>
<body style= "Font-family:arial" >
<HR size= "1"/>
<form runat= "Server" >
<asp Tutorial: DataGrid id= "DataGrid1" runat= "Server" enableviewstate= "False" forecolor= "Black" backcolor= "white" cellpadding= "3" gridlines= "None" cellspacing= "1" >
<itemstyle backcolor= "#DEDFDE" ></ItemStyle>
</asp:datagrid>
</form>
</body>

Other methods


1 System.Web.HttpPostedFile input = request.files[0];
2
3 if (input!= null && input. ContentLength!= 0)
4 {
5 String path = input. Filename.tostring ();
6 System.IO.StreamReader reader = new System.IO.StreamReader (path);
7
8 reader. Peek ();
9 while (reader. Peek () > 0)
10 {
One string str = reader. ReadLine ();
string[] split = str. Split (', ');
if (Split[0]!= "" && split[1]!= "")
14 {
System.Data.DataSet ds = new DataSet ();
System.Data.DataRow dr = ds. Tables[0]. NewRow ();
Dr[0] = split[0];
DR[1] = split[1];
DS. Tables[0]. Rows.Add (DR);
20}
21}
22}

CODE 2:

Just done it directly when the table to read

SELECT *
Into theimporttable
From
OPENROWSET (' Msdasql ',
' Driver={microsoft Text Driver (*.txt; *.csv)};D Efaultdir=d:; Extensions=csv; ',
' SELECT * from Csvfile.csv ')

CODE 3:

OPENROWSET (' Msdasql ',
' Driver={microsoft Text Driver (*.txt; *.csv)};D Efaultdir=d:; Extensions=csv; ',
' SELECT * from Csvfile.csv ')

It's so complicated!

File as a database tutorial, using OLE driver

CODE 4:

1 int intcolcount = 0;
2 DataTable myDT = new DataTable ("Mytablename");
3
4 DataColumn MYDC
5 DataRow mydr;
6
7 String strpath = "";
8 String strline;
9 string[] aryline;
Ten
System.IO.StreamReader MYSR = new System.IO.StreamReader (strpath),
All
while (strline = MYSR. ReadLine ())!= null)
{
Aryline = strline. Split (new char[] {' | '});

Intcolcount = aryline. Length;
for (int i = 0; i < Aryline. Length; i++)
{
MYDC = new DataColumn (Aryline[i]);
myDT. Columns.Add (MYDC);
22}

myDR = myDT. NewRow ();        
for (int i = 0; i < Intcolcount i++)
{
Mydr[i] = aryline[i];
28 }
myDT. Rows.Add (MYDR);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.