zoukankan      html  css  js  c++  java
  • 泛型在CVS数据读取中的应用

    CVS是一个轻量级的数据库,文本之间是以 逗号分隔的。结合泛型,读取此种文件的方式如下:

    代码
    public List<Dictionary<string, string>> GetData(string filePath, out List<string> Column)
    {
    Column
    = new List<string>();
    List
    <Dictionary<string, string>> Data = new List<Dictionary<string, string>>();
    string strLine = "";
    string[] strArray;
    StreamReader sr
    = new StreamReader(filePath);
    strLine
    = sr.ReadLine();
    if (strLine == null || strLine == "")
    {
    return new List<Dictionary<string, string>>();
    }
    strArray
    = strLine.Split(',');
    foreach (string item in strArray)
    {
    Column.Add(item);
    }
    strLine
    = sr.ReadLine();
    while (strLine != null)
    {
    strArray
    = strLine.Split(',');
    Dictionary
    <string, string> dict = new Dictionary<string, string>();
    for (int i = 0; i < Column.Count; i++)
    {
    dict.Add(Column[i], strArray[i]);
    }
    Data.Add(dict);
    strLine
    = sr.ReadLine();
    }
    sr.Close();
    return Data;
    }

    调用方法如下:

      List<string> Column;
                List<Dictionary<string, string>> Date = GetData(this.textBox1.Text.Trim(), out Column);
                string str = "";
                foreach (Dictionary<string, string> item in Date)
                {
                    foreach (string item1 in Column)
                    {
                        str += item[item1] + "   ";
                    }
                    str += Environment.NewLine;
                       
                }
                this.textBox2.Text = str;

    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    SQL Server连接Oracle详细步骤
    SQLServer2012连接mysql5.5
    SQL Server的链接服务器技术
    2键盘录入Scanner
    1标识符
    电脑从新分盘(软件)
    Tomcat安装配置
    windows下安装和配置多个版本的JDK
    Myeclipse2014的安装
    Could not find acceptable representation
  • 原文地址:https://www.cnblogs.com/hfliyi/p/1903434.html
Copyright © 2011-2022 走看看