Backstory: I got a huge wall of sql code from a program which I want to use to dump data in excel. Executing this code from VBA produces a lot of errors and the chunk of code is to big and jumbled to go through.
Running the query in C# works fine so I've written a program which grabs the data and dumps in a file I call excelDump.csv
For ease of use in order to avoid having to run this program separately I call the program via vba macro. Once the program has executed I grab (in vba) the data in the dumb and populate my excel sheet.
The c# program gets called, the console shows everything I want, it closes with the correct exit code and I confirm this code in VBA before trying to open the dump.
Here is the code that I run in C#
public void DumpToExcel()
{
using (System.IO.StreamWriter fs = new System.IO.StreamWriter("excelDump.csv"))
{
for (int i = 0; i < myReader.FieldCount; i++)
{
string name = myReader.GetName(i);
if (name.Contains(","))
name = "\"" + name + "\"";
fs.Write(name + ",");
}
fs.WriteLine();
while (myReader.Read())
{
for (int i = 0; i < myReader.FieldCount; i++)
{
string value = myReader[i].ToString();
if (value.Contains(","))
value = "\"" + value + "\"";
fs.Write(value + ",");
}
fs.WriteLine();
}
fs.Close();
}
}
What I have tried:
At first I thought the problem was that the program was unable to overwrite the file so I tried deleting the file but no go. The program works fine when I run it from visual studio or the release build from exe. But calling it from shell doesnt create a new file. Using file.delete can delete the old one just fine.
I thought maybe the c# program finishes to quickly before the files is created but Ive waited before importing data and still nothing.