2

I am new to programming. I was hoping for some help to correct the below code.

I have a WinForms application using an Azure SQL database. overall I am trying to automatically import a CSV file as it arrives in a cdrive location.

I have researched and tried BCP but failed to get passed Azure's security on my own account...., I don’t think my syntax is correctly built. I have also looked into Blob storage option again without much luck. I need to do more research on these options.

if I apply the following directly to the command line

dtexec/f “C:\InboundWindow\ImportScript.dtsx 

I get a successful result outputted. With this, in mind, I have then dragged a fileSystemWatcher to my WinForms and then applied the following code.

private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e) {


  // Process.Start("cmd", @"/C dtexec/f “C:\InboundWindow\ImportScript.dtsx");
  Process p = new Process();
  p.StartInfo.FileName = "cmd.exe";
  p.StartInfo.Arguments = @ "/C dtexec/f “C:\InboundWindow\ImportScript.dtsx";
  p.StartInfo.RedirectStandardOutput = false;
  p.StartInfo.UseShellExecute = false;
  p.StartInfo.CreateNoWindow = false; //don't show the console at all
  p.Start();


  // FullPath is the new file's path.
  MessageBox.Show(string.Format("Created: {0} {1}", e.FullPath, e.ChangeType));


}

this is where it now fails I have tried many variations found on various forums however the .dtsx file is never imported to Azure SQL database. however, I get a return message stating a change in the folder.

Any help in highlighting where I am going wrong and correcting the above would be great. please bear with me as I am new to c# and programming in general. thanks

MICHAEL
  • 43
  • 9
  • You're missing a closing `"` for the `Arguments`: Try `p.StartInfo.Arguments = @"/C dtexec/f ""C:\InboundWindow\ImportScript.dtsx""";` See [here](https://stackoverflow.com/questions/2911073/how-can-i-put-quotes-in-a-string) for using double quotes and `@`. – Zer0 Feb 20 '19 at 14:40
  • thank you!! i am looking into this. cheers – MICHAEL Feb 20 '19 at 14:48
  • Zer0 - your edit has worked!! thankyou ever so much. kind regards michael – MICHAEL Feb 20 '19 at 14:54
  • I put it in an answer. Please mark it as accepted so the question is closed. Thanks. – Zer0 Feb 20 '19 at 14:57

2 Answers2

3

You're missing a closing " for the Arguments, try:

p.StartInfo.Arguments = @"/C dtexec/f ""C:\InboundWindow\ImportScript.dtsx""";

See here for using double quotes and @

Zer0
  • 7,191
  • 1
  • 20
  • 34
1
 private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
    {


       // Process.Start("cmd", @"/C dtexec/f “C:\InboundWindow\ImportScript.dtsx");
        Process p = new Process();
        p.StartInfo.FileName = "ImportScript.dtsx"; //Since this is the name of the file that's going to be started
        p.StartInfo.Arguments = @"/C dtexec/f “C:\InboundWindow\ImportScript.dtsx";
        p.StartInfo.RedirectStandardOutput = false;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = false;  //don't show the console at all
        p.Start();


        // FullPath is the new file's path.
        MessageBox.Show(string.Format("Created: {0} {1}", e.FullPath, e.ChangeType));


    }

I replaced the "FileName = "cmd.exe" with "ImportScript.dtsx". Try that. :) I'm not 100 percent sure, but from what I can see that's the wrong. (Maybe other can see another problem, but at least try it :) )

Putte
  • 328
  • 2
  • 17
  • thank you for your quick responce. i have ran the above and now get an error on p.Start(); System.ComponentModel.Win32Exception: 'The system cannot find the file specified. which i am currently review why. once agin cheers – MICHAEL Feb 20 '19 at 14:43
  • I'm not really sure what ".dtsx" file is, but is it supported by windows? So you can run the file format, once when I built an C# application I got the same error, and later on showed that I cannot run a .jar file from this operative system (which is windows) So. I'm not really sure about your .dtsx file, but I guess you should wait for another answer to get it clearified. – Putte Feb 20 '19 at 14:50