In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to store the content of the text file in DataSet", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to store the content of the text file in DataSet" this article.
There are many operations on files in the project, such as upload and download of files, compression and decompression of files and other IO operations. In .NET projects, DataSet,DataTable is often used for data caching.
The operation of the text file in the project is relatively simple, but if you need to write the contents of the text file to the cache of the system, the operation will be a little more cumbersome. Now summarize a more general method of caching the contents of a text file into the DataSet dataset.
Private DataSet _ iifSet; / convert the text file to DataSet / 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) } / read the row array and parse it into the row array in the table / String iif file of the dataset / private void FillSet (IReadOnlyList 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); } } /// /// 解析记录 /// /// /// 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.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 ("!") } / private void CreateTables (IReadOnlyList 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 DataTable (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; iTunes +) {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);} above is all the content of the article "how to store text file content in DataSet". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.