首页 > 代码库 > TXT文件转换成DataSet数据集
TXT文件转换成DataSet数据集
1 /// <summary> 2 /// TXT文件转换成DataSet数据集 3 /// </summary> 4 /// <param name="FilePath"></param> 5 /// <param name="TableName"></param> 6 /// <returns></returns> 7 private DataSet TextFileLoader(string FilePath, string TableName) 8 { 9 10 DataSet ds = new DataSet(); 11 DataTable dt = new DataTable(TableName); 12 13 FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read); 14 using (StreamReader reader = new StreamReader(fs, System.Text.Encoding.GetEncoding("UTF-8")))15 {16 //int fieldCount = 0; 17 string[] headers = reader.ReadLine().Split(‘,‘);18 int fieldCount = headers.Length;19 for (int i = 0; i < fieldCount; i++)20 {21 dt.Columns.Add(new DataColumn(headers[i], typeof(string)));22 }23 // read one line24 string strRead=""; 25 // split the read line into string array26 string[] strReadBuffer=null;27 bool flag = true;28 while (flag)29 {30 //read one line31 strRead = reader.ReadLine();32 // if not null33 if (!string.IsNullOrEmpty(strRead))34 {35 // split into string array36 strReadBuffer=strRead.Split(‘,‘);37 // creat a new datarow object38 DataRow dr = dt.NewRow();39 //copy strings into this datarow40 for (int i = 0; i < fieldCount; i++)41 {42 dr[i] = strReadBuffer[i];43 } 44 // add this row into data reader45 dt.Rows.Add(dr);46 }47 else48 {49 flag = false;50 }51 }52 ds.Tables.Add(dt);53 reader.Close();54 return ds;55 }56 57 }
TXT文件转换成DataSet数据集
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。