In. NET, how to store the content of text files to DataSet,. netdataset

Source: Internet
Author: User

In. NET, how to store the content of text files to DataSet,. netdataset

Preface

Project operations on text files are relatively simple, but if you need to write the content of text files into the system cache, operations will be a little complicated. Now we can summarize a general method to cache the content of text files into the DataSet. Let's look at the sample code.

Sample Code

Private DataSet _ iifSet; /// <summary> /// convert the text file to DataSet // </summary> /// <param name = "filePath"> </param> /// <returns> </returns> public DataSet Parse (string filePath) {if (string. isNullOrEmpty (filePath) {throw new ArgumentNullException (filePath);} try {_ iifSet = new DataSet (); var fileText = System. IO. file. readAllText (filePath); var lines = fileText. split ('\ n'); CreateTables (lines, _ iifSet ); FillSet (lines, _ iifSet); return _ iifSet;} catch (IOException ex) {throw new IOException (ex. message );}} /// <summary> /// read the row array and parse it into the dataset table // </summary> /// <param name = "lines"> String iif array of rows in the file </param> /// <param name = "set"> </param> private void FillSet (IReadOnlyList <string> lines, dataSet set) {for (var I = 0; I <lines. count; I ++) {if (IsTableHeader (lines [I]) {continue;} if (lines [I] = "" | Lines [I] = "\ r" | lines [I] = "\ n \ r" | lines [I] =" \ n ") {continue;} if (lines [I]. indexOf (";__ IMPORTED _", StringComparison. ordinal )! =-1) {continue;} var line = lines [I]; while (! IsFullLine (line, set) {I ++; line + = lines [I] ;}parserecord (line, set );}} /// <summary> /// resolution record /// </summary> /// <param name = "line"> </param> /// <param name = "set"> </param> private void ParseRecord (string line, dataSet set) {if (IsTableHeader (line) {return;} var tablename = line. split ('\ t') [0]; var parameters = CreateDataRowParams (line, set. tables [tablename]. columns. count); if (parameters. Length> 0) set. tables [tablename]. rows. add (parameters);} private bool IsFullLine (string line, DataSet set) {if (IsTableHeader (line) {return true;} var values = line. split ('\ t '). length; var tableName = line. split ('\ t') [0]; var columns = set. tables [tableName]. columns. count; return values> = columns;} private bool IsTableHeader (string tab) {return tab. startsWith ("! ");} /// <Summary> /// create a able /// </summary> /// <param name = "lines"> </param> /// <param name = "set"> </param> private void CreateTables (IReadOnlyList <string> lines, dataSet set) {foreach (var t in lines. where (IsTableHeader) {set. tables. add (CreateTable (t);} private DataTable CreateTable (string line) {var values = line. split ('\ t'); values [0] = values [0]. substring (1); var dt = new DataTab Le (values [0]); values [0] = null; foreach (var name in values) {if (string. isNullOrEmpty (name) continue; var dc = new DataColumn (name, typeof (string); try {dt. columns. add (dc);} catch (DuplicateNameException) {dc = new DataColumn (name + "_ duplicateCol" + dt. columns. count); dt. columns. add (dc) ;}} return dt;} public string GetTableName (string line) {var values = line. split ('\ t'); if (values [0 ]. StartsWith ("! ") {Values [0] = values [0]. substring (1);} return values [0];} public readonly static object [] EmptyStringArray ={}; private object [] CreateDataRowParams (string line, int maxLength) {var raw = line. split ('\ t'); var length = raw. length-1; if (length = 0 | maxLength = 0) return EmptyStringArray; if (length> maxLength) length = maxLength; var values = new string [length]; for (var I = 0; I <Length; I ++) {values [I] = raw [I + 1];} if (values [values. length-1]. endsWith ("\ n") {values [values. length-1] = values [values. length-1]. substring (0, values [values. length-1]. lastIndexOf ('\ n');} else if (values [values. length-1]. endsWith ("\ n \ r") {values [values. length-1] = values [values. length-1]. substring (0, values [values. length-1]. lastIndexOf ("\ n \ r", StringComparison. ordinal ));} Else if (values [values. length-1]. endsWith ("\ r") {values [values. length-1] = values [values. length-1]. substring (0, values [values. length-1]. lastIndexOf ('\ R');} return values;} protected virtual void Dispose (bool cleanAll) {_ iifSet ?. Dispose ();} public void Dispose () {Dispose (true); GC. SuppressFinalize (this );}

Summary

Now, the content of this article is over. The basic attributes and methods of some common dataset operations are not introduced here. I hope the content of this article will help you in your study or work.

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.