How to write stored procedure output directly to a file on an FTP without using local or temp files?

后端 未结 6 1421
醉梦人生
醉梦人生 2021-01-02 03:54

I want to get the results of a stored procedure and place them into a CSV file onto an FTP location.

The catch though is that I cannot create a local/temporary file

6条回答
  •  礼貌的吻别
    2021-01-02 04:26

    If you were allowed to implement CLR integration assemblies you could actually use FTP without having to write a temporary file:

    public static void DoQueryAndUploadFile(string uri, string username, string password, string filename)
    {
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(uri + "/" + filename);
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        ftp.Credentials = new System.Net.NetworkCredential(username, password);
    
        using(StreamWriter sw = new StreamWriter(ftp.GetRequestStream()))
        {
            // Do the query here then write to the ftp stream by iterating DataReader or other resultset, following code is just to demo concept:
            for (int i = 0; i < 100; i++)
            {
                sw.WriteLine("{0},row-{1},data-{2}", i, i, i);
            }
            sw.Flush();
        }
    }
    

提交回复
热议问题