拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 在控制台中显示串列项

在控制台中显示串列项

白鹭 - 2022-01-26 1973 0 0

我正在尝试通过控制台显示串列中的项目,但我不确定如何实作。

使用以下代码将资料从 SQL 服务器添加到串列中:

串列:

namespace SSIS_FileWatcher

{
    public class WikiList //custom list
    {
        public string Source { get; set; }
    }
}

将资料添加到串列的代码:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

namespace SSIS_FileWatcher
{
    public class getWikiData
    {
        public List<WikiList> Source()
        {
            using (SqlConnection connection = new SqlConnection(Helper.CnnVal("DEV-BRMSQL01")))
            {
                connection.Open();
                SqlCommand sqlCommand = new SqlCommand("SELECT source FROM [REFERENCE].[KRI_METRIC_WIKI_AND_INGESTION_SCHEDULE] ", connection);
                SqlDataReader reader = sqlCommand.ExecuteReader();
                while (reader.Read())
                {
                    List<WikiList> entries = new List<WikiList>();

                    while (reader.Read())
                    {
                        WikiList w = new WikiList();
                        w.Source = (string)reader["Source"];
                        //w.Metric_ID = (string)reader["Metric_ID"];
                        //w.name = (string)reader["name"];
                        entries.Add(w);
                    }
                }
            }

            return List<WikiList>;
        }
    }
}

主要代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Data.SqlClient;
using System.Data;

namespace SSIS_FileWatcher
{
    public class Worker : BackgroundService
    {
        public string path = @"\\p1l-nas-02\EntTechBURM_DATA\PRD\";

        private readonly ILogger<Worker> _logger;

        public Worker(ILogger<Worker> logger)
        {
            _logger = logger;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {

                try 
                {
                    _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                    //add code to display list in console

                    await Task.Delay(60*1000, stoppingToken);
                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Exception: {ex.Message}");
                }
               
            }
        }
    }
}

正如您在最后一个代码示例中看到的那样,我评论了我想在哪里显示我的串列中的结果。如果有人能告诉我如何做到这一点,将不胜感激。

uj5u.com热心网友回复:

首先,将 getWikiData 的回传型别设定为List<WikiList>(). (这是您在编辑后完成的)

然后在呼叫之后getWikiData.Source(),你可以像这样创建一个字符串串列:

var wikis = new getWikiData().Source();
string str = String.Join("\r\n", wikis.Select(x => x.Source));
Console.WriteLine(str);

如果要添加类似子弹的结构,可以通过以下方式实作:

var wikis = new getWikiData().Source();
if (wikis.Count > 0) 
{
  string str = "\t- "   String.Join("\r\n\t- ", wikis.Select(x => x.Source));
  Console.WriteLine(str);
}

在您发表评论后编辑

我修改了您的方法以使其正常作业,请注意您重命名了该方法:

    public List<WikiList> Source()
    {
        List<WikiList> entries = new List<WikiList>();

        using (SqlConnection connection = new SqlConnection(Helper.CnnVal("DEV-BRMSQL01")))
        {
            connection.Open();
            SqlCommand sqlCommand = new SqlCommand("SELECT source FROM [REFERENCE].[KRI_METRIC_WIKI_AND_INGESTION_SCHEDULE] ", connection);
            SqlDataReader reader = sqlCommand.ExecuteReader();
            while (reader.Read())
            {

                while (reader.Read())
                {
                    WikiList w = new WikiList();
                    w.Source = (string)reader["Source"];
                    //w.Metric_ID = (string)reader["Metric_ID"];
                    //w.name = (string)reader["name"];
                    entries.Add(w);
                }
            }
        }

        return entries;
    }

uj5u.com热心网友回复:

提供一种替代方法(Cedric 可以,但如果有很多资料,控制台中可能暂时没有发生任何事情;因此您可能希望在检索到的每一行时打印它):

如果是这样,您可以将类定义调整为:

public class WikiList //custom list
{
    public string Source { get; set; }
    
    public void WriteToConsole() 
    {
        Console.WriteLine(Source);
    }
}

允许getWikiData更改为:

public class getWikiData
{
    public List<WikiData> Entries {get;set;}

    public getWikiData()
    {
        using (SqlConnection connection = new SqlConnection(Helper.CnnVal("DEV-BRMSQL01")))
        {
            connection.Open();
            SqlCommand sqlCommand = new SqlCommand("SELECT source FROM [REFERENCE].[KRI_METRIC_WIKI_AND_INGESTION_SCHEDULE] ", connection);
            SqlDataReader reader = sqlCommand.ExecuteReader();
            while (reader.Read())
            {
                Entries = new List<WikiList>();

                WikiList w = new WikiList();
                w.Source = (string)reader["Source"];
                //w.Metric_ID = (string)reader["Metric_ID"];
                //w.name = (string)reader["name"];
                Entries.Add(w);
                w.WriteToConsole();
            }
        }
    }
}

这意味着您只需呼叫getWikiData您的作业方法:

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {

            try 
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                getWikiData wikiData = new getWikiData(); 

                await Task.Delay(60*1000, stoppingToken);
                
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
            }
           
        }
    }

uj5u.com热心网友回复:

您可以使用 JavaScriptSerializer 类(添加对 System.Web.Extensions 的参考):

using System.Web.Script.Serialization;

在那之后

var wikis = getWikiData();
var json = new JavaScriptSerializer().Serialize(wikis);
Console.WriteLine(json);

此外,您可以使用 Newtonsoft 参考 - https://www.newtonsoft.com/json

uj5u.com热心网友回复:

你不需要 WikiData 类

 public List<string> Source()
    {
        List<string> entries = new List<string>();

        using (SqlConnection connection = new SqlConnection(Helper.CnnVal("DEV-BRMSQL01")))
        {
             ...
           while (reader.Read()) entries.Add(reader["Source"].ToString());
            reader.Close();
         }
        return entries;
    }

以及如何显示

 var getWikiData= new getWikiData();
 var source =getWikiData.Source(); 
 Console.WriteLine(string.Join(Join("\n\r",source));

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *