我正在嘗試通過控制臺顯示串列中的專案,但我不確定如何實作。
使用以下代碼將資料從 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));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/407798.html
標籤:
