using System; using System.IO; using System.Net; using FluentFTP; using Oracle.ManagedDataAccess.Client; using System.Linq; using FluentFTP.Helpers; namespace FtpToOracleConsoleApp { class Program { static void Main(string[] args) { string ftpServerUri = "ftp://888.88.88.88"; string ftpUsername = "a"; string ftpPassword = "b"; string oracleConnectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=90.32.15.55)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=report)));User Id=sa;Password=abc;"; ProcessDirectory(ftpServerUri, ftpUsername, ftpPassword, oracleConnectionString); } static void ProcessDirectory(string directoryUri, string ftpUsername, string ftpPassword, string oracleConnectionString) { FtpWebRequest directoryRequest = (FtpWebRequest)WebRequest.Create(directoryUri); directoryRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; directoryRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword); FtpWebResponse directoryResponse = (FtpWebResponse)directoryRequest.GetResponse(); Stream responseStream = directoryResponse.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!string.IsNullOrEmpty(line)) { string name = ParseFileNameFromFtpResponse(line); bool isDirectory = CheckIfDirectory(line); if (isDirectory) { string newUri = directoryUri.TrimEnd('/') + "/" + name; ProcessDirectory(newUri, ftpUsername, ftpPassword, oracleConnectionString); } else { if (!string.IsNullOrEmpty(line) && line.EndsWith(".txt")) { DateTime modifiedTime = ParseModifiedTime(line); //查询24小时内的文件 if ((DateTime.Now - modifiedTime).TotalHours <= 24) { var parts = name.Split(new char[] { '_', '[', ']' }); // 提取 prodId (第一部分) string prodId = parts[0]; // 提取 lotId 并替换其中的 _ 为 # string lotId = parts[1] + "#" + parts[2].Split(new char[] { 'S' })[1]; // 提取并转换 dieCount int dieCount = int.Parse(parts[4]); InsertIntoOracle(oracleConnectionString, prodId, lotId, dieCount); } } } } } reader.Close(); directoryResponse.Close(); } static bool CheckIfDirectory(string ftpResponseLine) { // 根据FTP服务器的响应格式判断是否为目录 // 这是一个简化的示例,可能需要根据实际情况进行调整 return ftpResponseLine.StartsWith("d"); } static string ParseFileNameFromFtpResponse(string ftpResponseLine) { // FTP响应的格式可能因FTP服务器而异,这里是一个通用的解析方法 // 通常文件名是在行的末尾,且前面有一些空格分隔的字段 var parts = ftpResponseLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); return parts[parts.Length - 1]; } static DateTime ParseModifiedTime(string ftpResponseLine) { // 根据FTP服务器的响应格式解析修改时间 // 示例仅作为示意,可能需要调整 var parts = ftpResponseLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int currentYear = DateTime.Now.Year; int currentMonth = DateTime.Now.Month; // 构建完整的日期时间字符串 string fullDateTimeStr = $"{currentYear}-{currentMonth}-{parts[6]} {parts[7]}"; // 定义日期时间的格式 string format = "yyyy-M-dd HH:mm"; if (DateTime.TryParseExact(fullDateTimeStr, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime parsedDateTime)) { return parsedDateTime; } else { throw new FormatException("Invalid date time format."); } } static void InsertIntoOracle(string connectionString, string prodId,string lotId, int dieCount) { using (OracleConnection conn = new OracleConnection(connectionString)) { conn.Open(); // 检查记录是否存在 string checkSql = "SELECT dieCount FROM your_table WHERE prodId = :prodId AND lotId = :lotId"; OracleCommand checkCmd = new OracleCommand(checkSql, conn); checkCmd.Parameters.Add(new OracleParameter("prodId", prodId)); checkCmd.Parameters.Add(new OracleParameter("lotId", lotId)); object result = checkCmd.ExecuteScalar(); if (result != null && result != DBNull.Value) { // 记录存在 int existingDieCount = Convert.ToInt32(result); if (existingDieCount == 0) { // dieCount 为空,更新记录 string updateSql = "UPDATE your_table SET dieCount = :dieCount WHERE prodId = :prodId AND lotId = :lotId"; OracleCommand updateCmd = new OracleCommand(updateSql, conn); updateCmd.Parameters.Add(new OracleParameter("dieCount", dieCount)); updateCmd.Parameters.Add(new OracleParameter("prodId", prodId)); updateCmd.Parameters.Add(new OracleParameter("lotId", lotId)); updateCmd.ExecuteNonQuery(); } // 如果 dieCount 不为空,则不执行任何操作 } else { // 记录不存在,插入新记录 string insertSql = "INSERT INTO your_table (prodId, lotId, dieCount) VALUES (:prodId, :lotId, :dieCount)"; OracleCommand insertCmd = new OracleCommand(insertSql, conn); insertCmd.Parameters.Add(new OracleParameter("prodId", prodId)); insertCmd.Parameters.Add(new OracleParameter("lotId", lotId)); insertCmd.Parameters.Add(new OracleParameter("dieCount", dieCount)); insertCmd.ExecuteNonQuery(); } } } } }
标签:FTP,oracleConnectionString,string,C#,ftpPassword,操作,using,line,ftpUsername From: https://www.cnblogs.com/lhhb/p/17867392.html