I am trying to display the items in my list via the console and I'm not sure how to achieve that.
Data is added to a list from a SQL server with the following code:
List:
namespace SSIS_FileWatcher
{
public class WikiList //custom list
{
public string Source { get; set; }
}
}
Code to add data to list:
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>;
}
}
}
Main code:
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}");
}
}
}
}
}
As you can see in the last code sample, I commented where I would like to show the results from my list. If anyone can show me how to do this, it would be much appreciated.
CodePudding user response:
first of all, set the return type of getWikiData to List<WikiList>().
(this you have done after an edit)
Then after calling getWikiData.Source(), you can create a string list like so:
var wikis = new getWikiData().Source();
string str = String.Join("\r\n", wikis.Select(x => x.Source));
Console.WriteLine(str);
If you want to add a bullet like structure, this can be achieved this way:
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);
}
edit after your comment
I modified your method to work, beware that you renamed the method:
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;
}
CodePudding user response:
To provide an alternative method (Cedric's will work, but if there's lots of data it may look like nothing's happening in the console for a while; so you may desire to print each line as it's retrieved):
If so, you could tweak the class definition to:
public class WikiList //custom list
{
public string Source { get; set; }
public void WriteToConsole()
{
Console.WriteLine(Source);
}
}
Allowing getWikiData to change to:
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();
}
}
}
}
Meaning you just call getWikiData in your worker method:
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}");
}
}
}
CodePudding user response:
You could use the JavaScriptSerializer class (add reference to System.Web.Extensions):
using System.Web.Script.Serialization;
after that
var wikis = getWikiData();
var json = new JavaScriptSerializer().Serialize(wikis);
Console.WriteLine(json);
Also, you can use Newtonsoft ref- https://www.newtonsoft.com/json
CodePudding user response:
you don' t need WikiData class
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;
}
and how to display
var getWikiData= new getWikiData();
var source =getWikiData.Source();
Console.WriteLine(string.Join(Join("\n\r",source));
