0
我寫過這個來檢查表列中的文件名是否存在於ftp服務器目錄中(如果存在),那麼更新(使用更新查詢)其值爲1
的列Isexists將保持原樣。如何使此FtpWebRequest代碼更快?
我正在使用DataTable讀取每一行(文件名),然後使FtpWebRequest所有時間檢查 該文件是否存在不。
這種方式CheckIfFtpFileExists(string fileUri
)獲得呼叫的次數等於no。 DataTable中的行。
有什麼辦法可以讓FTP auth。只有一次,然後調用該函數。
任何建議如何提高我的代碼以提高速度。
namespace FTP_File_Check
{
class Program
{
static void Main(string[] args)
{
string connString = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DMSTG;Data Source=.";
string sql = @"SELECT full_file_name from Intellitrack.S_Sr_att_Delta_Consolidated_28082012 where Isexists=0";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
{
SqlConnection sqlConnection1 = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DMSTG;Data Source=.");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
if (CheckIfFtpFileExists("ftp://myftpaddressgoeshere.com/0708/" + row[col].ToString()))
{
// Found File Exists
cmd.CommandText = @"UPDATE Intellitrack.S_Sr_att_Delta_Consolidated_28082012
SET Isexists = 1
where full_file_name='" + row[col].ToString() + "'";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}
}
}
}
public static bool CheckIfFtpFileExists(string fileUri)
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(fileUri);
request.Credentials = new NetworkCredential("myuserid", "mypassword");
request.KeepAlive = true;
request.ConnectionGroupName = "file";
request.ServicePoint.ConnectionLimit = 8;
request.UseBinary = true;
request.UsePassive = false;
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (FtpStatusCode.NotLoggedIn == response.StatusCode)
{
Console.WriteLine("Failed to connect to ftp server");
Console.ReadLine();
Environment.Exit(99);
}
else
{
if (FtpStatusCode.ActionNotTakenFileUnavailable == response.StatusCode)
{
return false;
}
}
}
return true;
}
}
}
是否都在同一個遠程目錄中的文件? – paul
@paul在一個目錄中的遠程服務器上是。 –