Home > Mobile >  Writing to a csv after extracting data from a db, cannot modify variable with data
Writing to a csv after extracting data from a db, cannot modify variable with data

Time:04-26

using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.IO;

namespace writingCSV
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var PatientEmails = await GetPatients();
            Type myType = PatientEmails.GetType();
            Console.WriteLine(myType);
        }

        public class PatientSurvey: IDisposable
        {
            public string Location { get; set; }
            public string Email { get; set; }

            public void Dispose()
            {
                throw new NotImplementedException();
            }
        }

        static public async Task<IEnumerable<PatientSurvey>> GetPatients()
        {
            var connectionString = "SERVER INFORMATION";
            
            using (var cnn = new SqlConnection(connectionString))
            {
                cnn.Open();

                var patientSurveys = await cnn.QueryAsync<PatientSurvey>("STORED PROC NAME",
                                null,
                                commandTimeout: 120,
                                commandType: CommandType.StoredProcedure);

                return patientSurveys;
            }
        }
    }
}

I'm attempting to write data to a CSV file from a database. I've successfully connected to the db and extracted the data into a C# object, but I cannot figure out how to modify my data so I can actually write it into the file. The PatientEmails variable has the data within it, but it seems like it's just an instance of the PatientSurvey class.

If I run my variable through a foreach loop, it prints out writingCSV.Program.PatientSurvey for each time it loops.

CodePudding user response:

I don't see any problem. GetPatients returns an IEnumerable of PatientSurvey so indeed looping on the list just print out writingCSV.Program.PatientSurvey. What would you expect? If your goal is to print patients email then you should write something like

foreach (var p in patientEmails) {
   Console.WriteLine(p.Email);
}

  • Related