Hello people,
Good afternoon!
In this post, I will talk again about CLR (C #), demonstrating yet another usefulness of this powerful feature of SQL Server, which is the integration between database and FTP servers, either to download or upload files between a local or network folder and the FTP server.
This feature is especially useful when you need integrations with other companies, where you need to export data and make it available on an external FTP from another company or import files from an FTP and import to your network or database.
To return alert messages and CLR error messages to the database, I use the Return class, which is available in the post. SQL Server - How to send warnings and error messages to the bank through CLR (C #) and is a dependency of this FTP class.
In all routines, I put a "retry" of up to 10 attempts in cases of connection failure or timeout, so that your routine does not show failure due to any instability in the network (this has helped me a LOT).
If you do not know the CLR or do not know where to start, see more accessing the post Introduction to SQL Common Language Runtime (CLR) in SQL Server.
Base Class - FTPControle.cs
View more details, usage examples, and source codeAlthough the code is quite large, it is not a complex class, and can be easily understood.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 |
using System; using System.Collections.Generic; using System.Data.SqlTypes; using System.Globalization; using System.Net; using System.IO; namespace Bibliotecas.Model { public class FTPControle { public string Host { get; set; } public string Login { get; set; } public string Senha { get; set; } private FtpWebRequest _ftp; public FTPControle(string host, string login, string senha) { Host = host; Login = login; Senha = senha; } public void ApagarArquivos(string pastaFtp, List<string> arquivos) { foreach (var arquivo in arquivos) { var arquivoFtp = Host + pastaFtp + arquivo; _ftp = (FtpWebRequest)WebRequest.Create(arquivoFtp); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.DeleteFile; _ftp.GetResponse(); } } public void ApagarArquivo(string pastaFtp, string arquivo) { var arquivoFtp = Host + pastaFtp + arquivo; _ftp = (FtpWebRequest)WebRequest.Create(arquivoFtp); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.DeleteFile; _ftp.GetResponse(); } public List<string> ListarArquivos(string pastaFtp, string filtro) { var _pastaFtp = Host + pastaFtp; _ftp = (FtpWebRequest)WebRequest.Create(_pastaFtp); var arquivos = new List<string>(); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.ListDirectory; using (var response = (FtpWebResponse) _ftp.GetResponse()) { using (var responseStream = response.GetResponseStream()) { if (responseStream != null) { using (var r = new StreamReader(responseStream)) { var s = r.ReadLine(); while (s != null) { arquivos.Add(s); s = r.ReadLine(); } } } } } return arquivos.FindAll( nome => nome.Contains(filtro.Replace("*", "")) ); } public List<string> ListarArquivosCompleto(string pastaFtp, string filtro) { var _pastaFtp = Host + pastaFtp; _ftp = (FtpWebRequest) WebRequest.Create(_pastaFtp); var arquivos = new List<string>(); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails; using (var response = (FtpWebResponse) _ftp.GetResponse()) { using (var responseStream = response.GetResponseStream()) { if (responseStream != null) { using (var r = new StreamReader(responseStream)) { var s = r.ReadLine(); while (s != null) { arquivos.Add(s); s = r.ReadLine(); } } } } } return arquivos.FindAll( nome => nome.Contains(filtro.Replace("*", "")) ); } private static string GetOctalPermission(string Ds_Permissao) { switch (Ds_Permissao) { case "---": return "0"; case "--x": return "1"; case "-w-": return "2"; case "-wx": return "3"; case "r--": return "4"; case "r-x": return "5"; case "rw-": return "6"; case "rwx": return "7"; default: return ""; } } private static int GetMonth(string Ds_Mes) { switch (Ds_Mes.ToLower()) { case "jan": return 1; case "feb": return 2; case "mar": return 3; case "apr": return 4; case "may": return 5; case "jun": return 6; case "jul": return 7; case "aug": return 8; case "sep": return 9; case "oct": return 10; case "nov": return 11; case "dec": return 12; default: return 0; } } private static string GetMonthName(string Ds_Mes) { switch (Ds_Mes) { case "1": return "jan"; case "2": return "feb"; case "3": return "mar"; case "4": return "apr"; case "5": return "may"; case "6": return "jun"; case "7": return "jul"; case "8": return "aug"; case "9": return "sep"; case "10": return "oct"; case "11": return "nov"; case "12": return "dez"; default: return ""; } } public FTPArquivoListar ListarArquivosCompleto_Parse(string arquivo, string Ds_Pasta_FTP, int Nr_Contador) { try { var dir = (arquivo.Substring(0, 1) == "d") ? "d" : "f"; var owner = ""; var group = ""; var ownerSec = ""; var groupSec = ""; var everyoneSec = ""; var size = 0; var month = ""; var year = ""; var day = ""; var filename = ""; SqlDateTime data; if (arquivo.Substring(15, 2).ToUpper() == "AM" || arquivo.Substring(15, 2).ToUpper() == "PM") { dir = arquivo.Contains("<DIR>") ? "d" : "f"; int.TryParse(arquivo.Substring(29, 9).Trim(), out size); month = arquivo.Substring(0, 2); day = arquivo.Substring(3, 2); year = arquivo.Substring(6, 2); year = (int.Parse(year) > DateTime.Now.Year ? "19" : "20") + year; var hora = int.Parse(arquivo.Substring(10, 2)) + (arquivo.Substring(15, 2).ToUpper() == "PM" && int.Parse(arquivo.Substring(10, 2)) < 12 ? 12 : 0); var minuto = int.Parse(arquivo.Substring(13, 2)); data = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day), hora, minuto, 0); filename = arquivo.Substring(39, arquivo.Length - 39).Trim(); } else { var palavras = arquivo.Split(' '); var contador = 1; ownerSec = arquivo.Substring(1, 3).Trim(); groupSec = arquivo.Substring(4, 3).Trim(); everyoneSec = arquivo.Substring(7, 3).Trim(); foreach (var palavra in palavras) { if (contador == 3) { owner = palavra.Trim(); } else if (contador == 4) { group = palavra.Trim(); } else if (contador == 5) { int.TryParse(palavra.Trim(), out size); } else if (contador == 6) { month = palavra.ToLower().Trim(); } else if (contador == 7) { day = palavra.Trim(); } else if (contador == 8) { year = palavra.Trim(); } else { if (contador >= 9) { filename += palavra; } } if (!string.IsNullOrEmpty(palavra) || contador >= 9) contador++; } if (year.IndexOf(":", StringComparison.Ordinal) >= 0) { var mes = GetMonth(month); var time = year.Split(':'); data = mes > DateTime.Now.Month ? new SqlDateTime(DateTime.Now.Year - 1, mes, Convert.ToInt32(day), Convert.ToInt32(time[0]), Convert.ToInt32(time[1]), 0) : new SqlDateTime(DateTime.Now.Year, mes, Convert.ToInt32(day), Convert.ToInt32(time[0]), Convert.ToInt32(time[1]), 0); } else { var dataOrigem = DateTime.ParseExact(year + "-" + month + "-" + day, "yyyy-MMM-d", new CultureInfo("en-US")); data = Convert.ToDateTime(dataOrigem.ToString("dd/MM/yyyy", new CultureInfo("pt-BR"))); } } if (filename == "." || filename == "..") { return new FTPArquivoListar( 0, "Diretorio", Ds_Pasta_FTP + "/", filename, //arquivo, size, SqlDateTime.Null, ownerSec + groupSec + everyoneSec, "0" + GetOctalPermission(ownerSec) + GetOctalPermission(groupSec) + GetOctalPermission(everyoneSec), owner, group ); } return new FTPArquivoListar( Nr_Contador, (dir == "d" ? "Diretorio" : "Arquivo"), Ds_Pasta_FTP + "/", filename, //arquivo, size, data, ownerSec + groupSec + everyoneSec, "0" + GetOctalPermission(ownerSec) + GetOctalPermission(groupSec) + GetOctalPermission(everyoneSec), owner, group ); } catch (Exception e) { return new FTPArquivoListar( 0, "Erro", arquivo, e.Message, //arquivo, 0, SqlDateTime.Null, "", "000", "", "" ); } } public int BaixarArquivos(string pastaFtp, List<string> arquivos, string pastaLocal) { var buffer = new byte[2048]; foreach (var arquivo in arquivos) { var arquivoFtp = Host + pastaFtp + arquivo; var arquivoLocal = pastaLocal + arquivo; _ftp = (FtpWebRequest)WebRequest.Create(arquivoFtp); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.DownloadFile; using (var response = (FtpWebResponse)_ftp.GetResponse()) { using (var responseStream = response.GetResponseStream()) { using (var fs = new FileStream(arquivoLocal, FileMode.Create)) { if (responseStream != null) { var read = responseStream.Read(buffer, 0, buffer.Length); ; while (read != 0) { fs.Write(buffer, 0, read); read = responseStream.Read(buffer, 0, buffer.Length); } } fs.Flush(); } } } } return arquivos.Count; } public void BaixarArquivo(string pastaFtp, string arquivo, string arquivoLocal) { var buffer = new byte[2048]; var arquivoFtp = Host + pastaFtp + arquivo; var _arquivoLocal = arquivoLocal; _ftp = (FtpWebRequest) WebRequest.Create(arquivoFtp); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.DownloadFile; using (var response = (FtpWebResponse) _ftp.GetResponse()) { using (var responseStream = response.GetResponseStream()) { using (var fs = new FileStream(_arquivoLocal, FileMode.Create)) { if (responseStream != null) { var read = responseStream.Read(buffer, 0, buffer.Length); ; while (read != 0) { fs.Write(buffer, 0, read); read = responseStream.Read(buffer, 0, buffer.Length); } } fs.Flush(); } } } } public int SubirArquivos(List<string> arquivos, string pastaFtp) { foreach (var arquivo in arquivos) { string nomeArquivo; if (arquivo.LastIndexOf(@"/", StringComparison.Ordinal) > 0) nomeArquivo = arquivo.Substring(arquivo.LastIndexOf(@"/", StringComparison.Ordinal),arquivo.Length - arquivo.LastIndexOf(@"/", StringComparison.Ordinal)); else if (arquivo.LastIndexOf(@"\", StringComparison.Ordinal) > 0) nomeArquivo = arquivo.Substring(arquivo.LastIndexOf(@"\", StringComparison.Ordinal), arquivo.Length - arquivo.LastIndexOf(@"\", StringComparison.Ordinal)); else return -1; var arquivoFtp = Host + pastaFtp + nomeArquivo; var arquivoLocal = arquivo; _ftp = (FtpWebRequest)WebRequest.Create(arquivoFtp); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.UploadFile; _ftp.Timeout = 600000; byte[] buffer; using (var stream = File.OpenRead(arquivoLocal)) { buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); } using (var reqStream = _ftp.GetRequestStream()) { reqStream.Write(buffer, 0, buffer.Length); } } return arquivos.Count; } public void SubirArquivo(string arquivoLocal, string pastaFtp, string nomeArquivoRemoto) { var arquivoFtp = Host + pastaFtp + nomeArquivoRemoto; var _arquivoLocal = arquivoLocal; _ftp = (FtpWebRequest)WebRequest.Create(arquivoFtp); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.UploadFile; _ftp.Timeout = 600000; byte[] buffer; using (var stream = File.OpenRead(_arquivoLocal)) { buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); } using (var reqStream = _ftp.GetRequestStream()) { reqStream.Write(buffer, 0, buffer.Length); } } public FtpWebResponse CriaDiretorio(string pastaFtp) { var arquivoFtp = Host + "/" + pastaFtp; _ftp = (FtpWebRequest) WebRequest.Create(arquivoFtp); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.MakeDirectory; using (var response = (FtpWebResponse) _ftp.GetResponse()) { return response; } } public FtpWebResponse ApagaArquivo(string pastaFtp, string arquivoRemoto) { var arquivoFtp = Host + "/" + pastaFtp + "/" + arquivoRemoto; _ftp = (FtpWebRequest) WebRequest.Create(arquivoFtp); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.DeleteFile; using (var response = (FtpWebResponse) _ftp.GetResponse()) { return response; } } public FtpWebResponse ApagaDiretorio(string pastaFtp, bool apagaArquivos) { pastaFtp = ("/" + pastaFtp + "/").Replace("//", "/"); if (apagaArquivos) { var filesList = ListarArquivos(pastaFtp, "*.*"); foreach (var file in filesList) { ApagaArquivo(pastaFtp, file); } } var arquivoFtp = Host + pastaFtp; _ftp = (FtpWebRequest)WebRequest.Create(arquivoFtp); IniciarMetodo(); _ftp.Method = WebRequestMethods.Ftp.RemoveDirectory; using (var response = (FtpWebResponse) _ftp.GetResponse()) { return response; } } private void IniciarMetodo() { //_ftp.Proxy = new WebProxy(Servidor.Ds_Proxy_Url, Servidor.Ds_Proxy_Porta) { Credentials = new NetworkCredential(Servidor.Ds_Proxy_Usuario, Servidor.Ds_Proxy_Senha, Servidor.Ds_Proxy_Dominio) }; _ftp.Proxy = null; _ftp.Credentials = new NetworkCredential(Login, Senha); _ftp.KeepAlive = false; _ftp.UseBinary = true; _ftp.UsePassive = true; } } public class FTPArquivoListar { public SqlInt32 Nr_Linha; public SqlString Nm_Arquivo; public SqlString Nm_Diretorio; public SqlString Fl_Tipo; public SqlInt64 Qt_Tamanho; public SqlDateTime Dt_Criacao; public SqlString Ds_Permissao; public SqlString Ds_Permissao_Octal; public SqlString Ds_Proprietario; public SqlString Ds_Grupo; public FTPArquivoListar(SqlInt32 nrLinha, SqlString nmArquivo, SqlString nmDiretorio, SqlString flTipo, SqlInt64 qtTamanho, SqlDateTime dtCriacao, SqlString dsPermissao, SqlString dsPermissaoOctal, SqlString dsProprietario, SqlString dsGrupo) { Nr_Linha = nrLinha; Nm_Arquivo = nmArquivo; Nm_Diretorio = nmDiretorio; Fl_Tipo = flTipo; Qt_Tamanho = qtTamanho; Dt_Criacao = dtCriacao; Ds_Permissao = dsPermissao; Ds_Permissao_Octal = dsPermissaoOctal; Ds_Proprietario = dsProprietario; Ds_Grupo = dsGrupo; } } } |
How to List Files from an FTP by SQL Server
View more details, usage examples, and source codeThe filter parameter allows you to inform the name of the file that you want to filter or use a Wildcard to return only the files that meet the informed criteria (Ex: “*” returns all files, “* .txt” returns all files ending with. txt, etc.)
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
using System.Data; using System.Data.SqlTypes; using System.Net; using System.Threading; using Microsoft.SqlServer.Server; using Bibliotecas.Model; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void stpFTP_Arquivo_Listar(SqlString host, SqlString pastaFtp, SqlString filtro, SqlString login, SqlString senha) { const int tentativas = 10; var sucesso = false; for (var i = 1; i <= tentativas; i++) { try { var ftp = new FTPControle(host.Value, login.Value, senha.Value); var arquivos = ftp.ListarArquivos(pastaFtp.Value, filtro.Value); var pipe = SqlContext.Pipe; var cols = new SqlMetaData[1]; cols[0] = new SqlMetaData("Nm_Arquivo", SqlDbType.NVarChar, 1024); var rec = new SqlDataRecord(cols); if (pipe == null) return; pipe.SendResultsStart(rec); foreach (var file in arquivos) { rec.SetSqlString(0, new SqlString(file)); pipe.SendResultsRow(rec); } pipe.SendResultsEnd(); sucesso = true; } catch (WebException e) { if (e.Status == WebExceptionStatus.Timeout || e.Status == WebExceptionStatus.ConnectFailure || e.Status == WebExceptionStatus.SendFailure || e.Status == WebExceptionStatus.ReceiveFailure || e.Status == WebExceptionStatus.PipelineFailure || e.Status == WebExceptionStatus.ConnectionClosed) { if (i < tentativas) Retorno.Mensagem($"Erro na tentativa {i}: {e.Message}"); else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } if (sucesso) break; Thread.Sleep(10000); } } }; |
Function for listing files from an FTP by SQL Server
View more details, usage examples, and source codeIn addition, it can be easily used to filter with WHERE, use JOINS, ORDER BY, SELECT * INTO #Table, etc., giving much more flexibility in its use compared to the previous Stored Procedure.
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
using System.Collections; using System.Collections.Generic; using System.Data.SqlTypes; using System.Net; using System.Threading; using Bibliotecas.Model; public partial class UserDefinedFunctions { [Microsoft.SqlServer.Server.SqlFunction( FillRowMethodName = "FillRow_FTP_Arquivos_Listar", TableDefinition = "Nr_Linha INT, Fl_Tipo NVARCHAR(20), Nm_Diretorio NVARCHAR(500), Nm_Arquivo NVARCHAR(500), " + "Qt_Tamanho BIGINT, Dt_Criacao DATETIME, Ds_Permissao NVARCHAR(20), Ds_Permissao_Octal NVARCHAR(4), " + "Ds_Proprietario NVARCHAR(20), Ds_Grupo NVARCHAR(20)" )] public static IEnumerable fncFTP_Arquivos_Listar(string Ds_Hostname, string Ds_Usuario, string DsSenha, string Ds_Pasta_FTP, string Ds_Filtro) { var ftpArquivoListarCollection = new ArrayList(); if (string.IsNullOrEmpty(Ds_Hostname)) return ftpArquivoListarCollection; var ftp = new FTPControle(Ds_Hostname, Ds_Usuario, DsSenha); var arquivos = new List<string>(); const int tentativas = 10; var sucesso = false; for (var i = 1; i <= tentativas; i++) { try { arquivos = ftp.ListarArquivosCompleto(Ds_Pasta_FTP, Ds_Filtro); sucesso = true; } catch (WebException e) { if (e.Status == WebExceptionStatus.Timeout || e.Status == WebExceptionStatus.ConnectFailure || e.Status == WebExceptionStatus.SendFailure || e.Status == WebExceptionStatus.ReceiveFailure || e.Status == WebExceptionStatus.PipelineFailure || e.Status == WebExceptionStatus.ConnectionClosed) { if (i < tentativas) Retorno.Mensagem($"Erro na tentativa {i}: {e.Message}"); else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } if (sucesso) break; Thread.Sleep(10000); } var contador = 1; foreach (var arquivo in arquivos) { if (arquivo.Substring(0, 1) != "d") continue; var retorno = ftp.ListarArquivosCompleto_Parse(arquivo, Ds_Pasta_FTP, contador); if (retorno.Nr_Linha.Value == 0) continue; ftpArquivoListarCollection.Add(retorno); contador++; } foreach (var arquivo in arquivos) { if (arquivo.Substring(0, 1) == "d") continue; var retorno = ftp.ListarArquivosCompleto_Parse(arquivo, Ds_Pasta_FTP, contador); ftpArquivoListarCollection.Add(retorno); contador++; } return ftpArquivoListarCollection; } protected static void FillRow_FTP_Arquivos_Listar(object objFTPArquivoListar, out SqlInt32 nrLinha, out SqlString nmArquivo, out SqlString nmDiretorio, out SqlString flTipo, out SqlInt64 qtTamanho, out SqlDateTime dtCriacao, out SqlString dsPermissao, out SqlString dsPermissaoOctal, out SqlString dsProprietario, out SqlString dsGrupo) { var ftpArquivoListar = (FTPArquivoListar) objFTPArquivoListar; nrLinha = ftpArquivoListar.Nr_Linha; nmArquivo = ftpArquivoListar.Nm_Arquivo; nmDiretorio = ftpArquivoListar.Nm_Diretorio; flTipo = ftpArquivoListar.Fl_Tipo; qtTamanho = ftpArquivoListar.Qt_Tamanho; dtCriacao = ftpArquivoListar.Dt_Criacao; dsPermissao = ftpArquivoListar.Ds_Permissao; dsPermissaoOctal = ftpArquivoListar.Ds_Permissao_Octal; dsProprietario = ftpArquivoListar.Ds_Proprietario; dsGrupo = ftpArquivoListar.Ds_Grupo; } } |
How to download a file from an FTP server through SQL Server
View more details, usage examples, and source codeAn especially useful parameter is @apagarRemoto, which when the value “1” is informed, downloads the files that meet the criteria of the informed filters and, immediately afterwards, deletes these files from the FTP server.
Examples of use:
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
using System.Data.SqlTypes; using System.IO; using System.Net; using System.Threading; using Bibliotecas.Model; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void stpFTP_Arquivo_Download(SqlString host, SqlString pastaFtp, SqlString filtro, SqlString login, SqlString senha, SqlString pastaLocal, SqlBoolean apagarRemoto) { #region tratamento de entradas try { if (!Directory.Exists(pastaLocal.Value)) { Retorno.Erro("Pasta local especificada não existe ou inacessivel."); return; } } catch { Retorno.Erro("Erro ao converter pasta local especificada."); return; } #endregion const int tentativas = 10; var sucesso = false; for (var i = 1; i <= tentativas; i++) { try { var ftp = new FTPControle(host.Value, login.Value, senha.Value); var arquivos = ftp.ListarArquivos(pastaFtp.Value, filtro.Value); var qntArquivos = ftp.BaixarArquivos(pastaFtp.Value, arquivos, pastaLocal.Value); if (apagarRemoto.Value) { ftp.ApagarArquivos(pastaFtp.Value, arquivos); } Retorno.Mensagem("Total de: (" + qntArquivos + ") arquivos foram baixados para: " + pastaLocal.Value); sucesso = true; } catch (WebException e) { if (e.Status == WebExceptionStatus.Timeout || e.Status == WebExceptionStatus.ConnectFailure || e.Status == WebExceptionStatus.SendFailure || e.Status == WebExceptionStatus.ReceiveFailure || e.Status == WebExceptionStatus.PipelineFailure || e.Status == WebExceptionStatus.ConnectionClosed) { if (i < tentativas) Retorno.Mensagem($"Erro na tentativa {i}: {e.Message}"); else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } if (sucesso) break; Thread.Sleep(10000); } } }; |
How to send a file to an FTP server through SQL Server
View more details, usage examples, and source codeAgain, SP lets you specify the file name in the @filter parameter to send a specific file, or use wildcards to perform filename filtering and send multiple files in a single Stored Procedure run.
Example of use:
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
using System.Collections.Generic; using System.Data.SqlTypes; using System.IO; using System.Net; using System.Threading; using Bibliotecas.Model; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void stpFTP_Arquivo_Upload(SqlString host, SqlString pastaFtp, SqlString login, SqlString senha, SqlString pastaLocal, SqlString filtro) { #region tratamento de entradas try { if (!Directory.Exists(pastaLocal.Value)) { Retorno.Erro("Pasta local especificada não existe ou inacessivel."); return; } } catch { Retorno.Erro("Erro ao converter pasta local especificada."); return; } List<string> listaArquivos; try { var arquivos = Directory.GetFiles(pastaLocal.Value, filtro.Value); listaArquivos = new List<string>(arquivos); if (listaArquivos.Count == 0) { Retorno.Mensagem("(0) arquivos atendem o requisito de filtragem na pasta especificada."); return; } } catch { Retorno.Erro("Erro ao listar arquivos da pasta especificada."); return; } #endregion const int tentativas = 10; var sucesso = false; for (var i = 1; i <= tentativas; i++) { try { var ftp = new FTPControle(host.Value, login.Value, senha.Value); var qntArquivos = ftp.SubirArquivos(listaArquivos, pastaFtp.Value); if (qntArquivos != -1) { Retorno.Mensagem("Total de: (" + qntArquivos + ") arquivos foram enviados para: " + pastaFtp.Value); sucesso = true; } else Retorno.Erro("Erro : Diretório do arquivo local inválido!"); } catch (WebException e) { if (e.Status == WebExceptionStatus.Timeout || e.Status == WebExceptionStatus.ConnectFailure || e.Status == WebExceptionStatus.SendFailure || e.Status == WebExceptionStatus.ReceiveFailure || e.Status == WebExceptionStatus.PipelineFailure || e.Status == WebExceptionStatus.ConnectionClosed) { if (i < tentativas) Retorno.Mensagem($"Erro na tentativa {i}: {e.Message}"); else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } if (sucesso) break; Thread.Sleep(10000); } } } |
How to create a directory on an FTP server through SQL Server
View more details, usage examples, and source codeUsing the functions of listing files and directories, you can identify whether or not the desired directory exists, and if not, create it using this SP.
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
using System.Data.SqlTypes; using Bibliotecas.Model; using System.Net; using System.Threading; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void stpFTP_Cria_Diretorio(SqlString host, SqlString login, SqlString senha, SqlString pastaFtp) { const int tentativas = 10; var sucesso = false; for (var i = 1; i <= tentativas; i++) { try { var ftp = new FTPControle(host.Value, login.Value, senha.Value); using (var response = ftp.CriaDiretorio(pastaFtp.Value)) { if (response.StatusCode == FtpStatusCode.PathnameCreated) { Retorno.Mensagem("Diretório " + pastaFtp.Value + " criado com sucesso"); sucesso = true; } else { Retorno.Erro(response.StatusDescription); } } } catch (WebException e) { if (e.Status == WebExceptionStatus.Timeout || e.Status == WebExceptionStatus.ConnectFailure || e.Status == WebExceptionStatus.SendFailure || e.Status == WebExceptionStatus.ReceiveFailure || e.Status == WebExceptionStatus.PipelineFailure || e.Status == WebExceptionStatus.ConnectionClosed) { if (i < tentativas) Retorno.Mensagem($"Erro na tentativa {i}: {e.Message}"); else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } if (sucesso) break; Thread.Sleep(10000); } } }; |
How to Delete a File from an FTP Server by SQL Server
View more details, usage examples, and source codeThis is not due to technical impossibility, I just found it more “safe” not to allow deleting multiple files using a filter, but nothing to stop you from changing this Procedure to allow this or using the Stored Procedure stpFTP_Apaga_Diretorio, which is just below.
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System.Data.SqlTypes; using Bibliotecas.Model; using System.Net; using System.Threading; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void stpFTP_Apaga_Arquivo(SqlString host, SqlString login, SqlString senha, SqlString pastaFtp, SqlString arquivoRemoto) { const int tentativas = 10; var sucesso = false; for (var i = 1; i <= tentativas; i++) { try { var ftp = new FTPControle(host.Value, login.Value, senha.Value); using (var response = ftp.ApagaArquivo(pastaFtp.Value, arquivoRemoto.Value)) { if (response.StatusCode == FtpStatusCode.CommandOK || response.StatusCode == FtpStatusCode.FileActionOK) { Retorno.Mensagem("Arquivo " + pastaFtp.Value + "/" + arquivoRemoto.Value + " apagado com sucesso"); sucesso = true; } else { Retorno.Erro(response.StatusDescription); } } } catch (WebException e) { if (e.Status == WebExceptionStatus.Timeout || e.Status == WebExceptionStatus.ConnectFailure || e.Status == WebExceptionStatus.SendFailure || e.Status == WebExceptionStatus.ReceiveFailure || e.Status == WebExceptionStatus.PipelineFailure || e.Status == WebExceptionStatus.ConnectionClosed) { if (i < tentativas) Retorno.Mensagem($"Erro na tentativa {i}: {e.Message}"); else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } if (sucesso) break; Thread.Sleep(10000); } } }; |
How to Delete a Directory from an FTP Server by SQL Server
View more details, usage examples, and source codeKeep in mind that if you enter the value of the @appileFiles = 0 parameter and there are files in the directory you are trying to delete, SP will return an error message stating that there are files in the directory.
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System.Data.SqlTypes; using Bibliotecas.Model; using System.Net; using System.Threading; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void stpFTP_Apaga_Diretorio(SqlString host, SqlString login, SqlString senha, SqlString pastaFtp, SqlBoolean apagaArquivos) { const int tentativas = 10; var sucesso = false; for (var i = 1; i <= tentativas; i++) { try { var ftp = new FTPControle(host.Value, login.Value, senha.Value); using (var response = ftp.ApagaDiretorio(pastaFtp.Value, apagaArquivos.Value)) { if (response.StatusCode == FtpStatusCode.FileActionOK) { Retorno.Mensagem("Diretório " + pastaFtp.Value + " apagado com sucesso"); sucesso = true; } else { Retorno.Erro(response.StatusDescription); } } } catch (WebException e) { if (e.Status == WebExceptionStatus.Timeout || e.Status == WebExceptionStatus.ConnectFailure || e.Status == WebExceptionStatus.SendFailure || e.Status == WebExceptionStatus.ReceiveFailure || e.Status == WebExceptionStatus.PipelineFailure || e.Status == WebExceptionStatus.ConnectionClosed) { if (i < tentativas) Retorno.Mensagem($"Erro na tentativa {i}: {e.Message}"); else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } else Retorno.Erro($"Erro : {e.Message}\n\nInner Exception: {e.InnerException}"); } if (sucesso) break; Thread.Sleep(10000); } } }; |
That's it folks!
I hope you enjoyed this post and even more!
sql server clr C # csharp download download read read upload upload files files list list integrate integrate integration integration server ftp server
sql server clr C # csharp download download read read upload upload files files list list integrate integrate integration integration server ftp server
Good morning, as I explained earlier I am new to C # development.
In class stpFTP_Archive_Listar unable to compile I have error in the snippet:
if (i <attempts)
Return.Message ($ "Error in attempt {i}: {e.Message}");
else
Return.Error ($ "Error: {e.Message} \ n \ nInner Exception: {e.InnerException}");
}
else
Return.Error ($ "Error: {e.Message} \ n \ nInner Exception: {e.InnerException}");
Error CS0103 The name 'Return' does not exist in the current context Libraries
Can you help me solve this? Thankful.
Luciano, you did not include the Return class. Take a look at the post, which I talk about her in the beginning 🙂
OK, Dirceu, I checked that. Grateful friend.