반응형

김종열

MS SQL Server MVP

엔트리브 소프트


그동안 회사사정으로 인해 조금 바빴는데요. 물론 지금도 바쁘지만 핑계삼아...
망중한으로 ftp upload를 하는 funtion을 하나 만들어봤습니다.
기능은 upload를 하고 성공(1), 실패(0)를 리턴하는 함수입니다.

이쯤되면 파일에 대한 작업이나 ftp등의 네트웍프로그램까지 확장이 되어버렸다는..
CLR의 끝은 자신이 하고자 하는 작업이나 상상력이 한계인듯하군요.. ㅋㅋ

다음은 c#코드입니다. 역시 설명은 드리지 않겠습니다.

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Net;

using System.Xml;

using System.Text;

 

public partial class UserDefinedFunctions

{

    [Microsoft.SqlServer.Server.SqlFunction]

    public static SqlInt32 UDF_UPLOAD(SqlString ftp_server

                                      , SqlString ftp_user

                                      , SqlString ftp_pwd

                                      , SqlString local_file

                                      , SqlString rmt_file

                                      , SqlInt32 ftp_mode)

    {

        /* Init Vairable */

        string ftpServer = Convert.ToString(ftp_server);

        string ftpUser = Convert.ToString(ftp_user);

        string ftpPwd = Convert.ToString(ftp_pwd);

        string localFile = Convert.ToString(local_file);

        string rmtFile = Convert.ToString(rmt_file);

        int ftpMode = Convert.ToInt32(Convert.ToString(ftp_mode)); /* asc(1), bin(else) */

 

 

        FileInfo fileinf = new FileInfo(localFile);

        string uri = "ftp://" + ftpServer + rmtFile;

        FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

        reqFtp.Credentials = new NetworkCredential(ftpUser, ftpPwd);

        reqFtp.KeepAlive = false;

        reqFtp.Method = WebRequestMethods.Ftp.UploadFile;

 

        if (ftpMode == 1)

        {

            reqFtp.UseBinary = false;

        }

        else

        {

            reqFtp.UseBinary = true;

        }

 

        reqFtp.ContentLength = fileinf.Length;

 

        int buffLength = 2048;

        byte[] buff = new byte[buffLength];

        int contentLen;

 

        FileStream fs = fileinf.OpenRead();

 

        try

        {

            Stream strm = reqFtp.GetRequestStream();

            contentLen = fs.Read(buff, 0, buffLength);

 

            while (contentLen != 0)

            {

                strm.Write(buff, 0, contentLen);

                contentLen = fs.Read(buff, 0, buffLength);

            }

            strm.Close();

            fs.Close();

 

            return 1;

        }

        catch

        {

            return 0;

        }       

    }

};

 

 

 




그리고 나서 역시 배포를 하고 나시면 database에서 그 명령을 실행할 수 있겠죠..

DECLARE @RET_up INT

SELECT @RET_up = DBO.UDF_UPLOAD('<test_ftp_server_name> or <server ip>'

                                 , 'id'

                                 , 'pwd'

                                 , 'upload_file_full_path'

                                 , 'remote_save_full_path'

                                 , 1) --1 asc, else bin

SELECT @RET_up 

 





참고파일 : udf_upload.cs
             udf_upload.sql

반응형

'연구개발 > CLR' 카테고리의 다른 글

CLR - Mail 보내기  (0) 2009.06.22
CLR - FTP download(FTP 2탄)  (0) 2009.06.22
CLR - EXCEL 파일 읽기  (0) 2009.06.22
CLR - 쿼리의 결과를 파일로 저장하기  (0) 2009.06.22
CLR - 10진수를 다른 진법의 수로 표현  (0) 2009.06.22

+ Recent posts