【问题标题】:Populate dataset from txt file从 txt 文件填充数据集
【发布时间】:2022-01-19 18:24:02
【问题描述】:

您好,我想创建一个数据集并使用我拥有的所有意大利名称的 txt 文件填充它,例如: 蕉

阿邦丹扎

阿凡达

阿布达齐奥

阿布达齐奥

阿邦迪亚

阿邦迪娜

没有空格。有没有简单的代码来做到这一点?谢谢。

【问题讨论】:

    标签: c# asp.net dataset c#-datatable


    【解决方案1】:

    尝试以下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication7
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] inputs = {"abaco","abbondanza","abbondanzia","abbondanzio","abbondazio","abbondia","abbondina"};
                DataTable dt = new DataTable();
                dt.Columns.Add("Italian Names", typeof(string));
    
                foreach (string input in inputs)
                {
                    dt.Rows.Add(new string[] { input });
                }
    
     
    
            }
        }
    }
    

    更新

      try
                {
                    // Create an instance of StreamReader to read from a file.
                    // The using statement also closes the StreamReader.
                    using (StreamReader sr = new StreamReader("C:\\Users\\Fabio\\Desktop\\dataset\\nomi_italiani.txt"))
                    {
                        string line;
                        // Read and display lines from the file until the end of
                        // the file is reached.
                        while ((line = sr.ReadLine()) != null)
                        {
                           listnamedataset = //HOW CAN I ADD EVERY ELEMENT TO THE LIST??
    
                        }
                    }
                }
                catch (Exception e)
                {
     
                }
    

    【讨论】:

    • 没有....在输入中我不仅有这些名字....我在一个文件中有 4k 个名字...
    • 因此修改代码以使用 StreamReader 从文件中读取名称。
    • 我是 C# 的菜鸟,所以在这里发布以帮助...你能更新你的代码来帮助我吗?
    • @jdweng 请检查我的答案中的更新。进入一段时间
    猜你喜欢
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多