0

I am trying to make a C# console application, I want to see my output as same as mssql format. I will take output as a csv or excell file. Could you please guide me how can I arrange my output?

SqlConnection conn = new SqlConnection("Server=sad;Database=*;User Id=*;Password=*;");
conn.Open();
string query = "select computername, version, os, from testdatabase";
SqlCommand cmd = new SqlCommand(query, conn);
                SqlDataReader reader = cmd.ExecuteReader();
 while (reader.Read())
                    {
//something should be here but I dont know what
}

reader.Close();
conn.Close();
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
themuh
  • 13
  • 1
  • 3
  • *All* beginner samples have a `Console.WriteLine("Hello World");` That will print an output. If you want to generate a CSV or Excel file, you'll have to wite the code. You'll find several dozens of duplicate questions – Panagiotis Kanavos Oct 25 '17 at 13:16
  • You will either need to write the code to generate a CSV or Excel file, or find a library that does it for you. Asking us to just give you the code is too broad. You need to research appropriate libraries yourself, try to implement them, then provide an [MCVE](https://stackoverflow.com/help/mcve) if you get stuck. I suggest you don't re-invent the wheel. – mason Oct 25 '17 at 13:22
  • 1
    Possible duplicate of [Export DataTable to excel with EPPlus](https://stackoverflow.com/questions/13669733/export-datatable-to-excel-with-epplus) – Panagiotis Kanavos Oct 25 '17 at 13:28
  • Check the duplicate. It takes just 3 lines of code to write a DataTable or collection of objects to an Excel file – Panagiotis Kanavos Oct 25 '17 at 13:29
  • I am able to use Console.Writeline but I am not able to take that output in csv format. I do not know how can I do this. Thats the reason why you are not seeing that part of code – themuh Oct 25 '17 at 13:58
  • @themuh *Did* you read the duplicate? Or try just search SO? You'll find a *lot* more duplicates, from the simplest that show how to write multiple values to a file, to asnwers that show how to use libraries like CsvHelper – Panagiotis Kanavos Oct 25 '17 at 14:00
  • @themu *why* write a program for this in the first place, if you don't know C#? SQL Server can export data to many formats, either through a wizard in SSMS or a scheduled job. You can also use the `bcp` command to export data to a flat file. – Panagiotis Kanavos Oct 25 '17 at 14:03
  • @themuh Check out this question for how to print a MySQL like table: https://stackoverflow.com/questions/856845/how-to-best-way-to-draw-table-in-console-app-c – Motomotes Oct 25 '17 at 23:43

1 Answers1

-1

A simple example of creating a CSV file would be like this:

List<string> outLines = new List<string>();

while (reader.Read())
{
   outLines.add(reader.GetValue(0).ToString() + "," +
                reader.GetValue(1).ToString() + "," +
                reader.GetValue(2).ToString());
}

System.IO.File.WriteAllLines("C:/outFile.csv",outLines.ToArray());
Motomotes
  • 4,111
  • 1
  • 25
  • 24
  • What happens if any of those values contain commas? Please do not make up your own CSV generating code - you'll get it wrong and you'll teach people to re-invent the wheel .There are plenty of well supported and battle-tested CSV libraries out there. – mason Oct 25 '17 at 15:01