<summary>////file stream to read CSV file///</summary>//<param name= "Files" > file name </param> ; <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 (); } }