CSV Import SQL Server database

Source: Internet
Author: User

    <summary>////file stream to read CSV file///</summary>//<param name= "Files" > file name </param&gt    ; <param name= "Headyes" > whether the first row is a column heading </param>//<param name= "span" > Delimiter </param>///<retur Ns></returns> public DataTable readcsvfiletotable (bool Headyes, char span) {//file path and filename Strin        G files = path + FileName;        DataTable dt = new DataTable ();        StreamReader FileReader = new StreamReader (files, encoding.default);            try {//is the first row (if Headyes is true, the first behavior is the header row) int lsi = 0;            Delimiter between columns char CV = span;                while (Filereader.endofstream = = false) {string line = Filereader.readline (); string[] y = line.                Split (CV);                    First Behavior Header line if (Headyes = = true) {//First line if (LSI = = 0) {for (int i = 0; i < y.length; i++) {dt. Columns.Add (Y[i]. Trim ().                        ToString ());                    } lsi++; }//Start with the second column for the data column else {DataRow dr = dt.                        NewRow (); for (int i = 0; i < y.length; i++) {dr[i] = Y[i].                        Trim (); } dt.                    Rows.Add (DR);                    }}//The first line is not a header row else {if (LSI = = 0)                            {for (int i = 0; i < y.length; i++) { Dt.                        Columns.Add ("Col" + i.tostring ());                    } lsi++; } DataRow dr = dt.                    NewRow (); for (int i = 0; i < y.length;i++) {Dr[i] = Y[i].                    Trim (); } dt.                Rows.Add (DR);        }}} catch (Exception ex) {throw ex;            } finally {filereader.close ();        Filereader.dispose ();    } return DT; }
private String conn = configurationmanager.connectionstrings["Constr"]. ToString ();
<summary>
Copy data from the specified table to the target database///</summary>// <param name= "DT" > Datasheet </param>// <param Name= "Outtime" > Time-out </param> //<returns></returns> public string Copytabletosql ( DataTable DT, int outtime, string tableName) { SqlBulkCopy SBC = new SqlBulkCopy (conn, sqlbulkcopyoptions.useinternaltransaction); Sbc. Bulkcopytimeout = Outtime; Sbc. Notifyafter = dt. Rows.Count; Try { SBC. DestinationTableName = TableName; Sbc. WriteToServer (DT); Return ""; } catch (Exception ex) { return ex. Message; } Finally { SBC. Close (); } }



 

Related Article

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.